【问题标题】:Remove rows if a specific columns contains anything other than numbers? [duplicate]如果特定列包含除数字以外的任何内容,则删除行? [复制]
【发布时间】:2019-01-15 12:40:20
【问题描述】:

我有一个这样的数据框:

df:

col1    col2
 1        4
 4       ab1
 5       1s,2
 6        5
 3        24
 5        xy

如果 col2 包含除数字以外的任何字符,则从数据框中删除这些行。

最终的数据框将如下所示:

col1    col2
 1        4
 6        5
 3        24  

如何有效地使用熊猫?

【问题讨论】:

  • df = df[df.id2.str.contains(r'^[0-9]$')]
  • df = df[df.col2.str.encode('utf-8').isna()]

标签: python string pandas dataframe


【解决方案1】:

试试这个

df=df[df.col2.apply(lambda x: x.isnumeric())]

【讨论】:

    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2020-08-22
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多