【问题标题】:How to drop multiple columns from a pandas dataframe if it contains any alphabet (non numeric)?如果它包含任何字母(非数字),如何从熊猫数据框中删除多列?
【发布时间】:2019-05-29 20:11:38
【问题描述】:

我已经使用 pyhive 从 hive 中提取数据到 python 中。现在我从导入的数据创建了一个数据框。数据框中所有列的 dtype 都是 object。我想知道删除数据框中包含字母(非数字)的任何列的最佳方法?

df.select_dtypes(['number']) 不起作用,因为我的所有列都是 dtype=object。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    以下将数字字符串转换为数字 dtypes 并从 DataFrame 中仅选择数字 dtypes。

    #convert number columns to numeric
    df=df.apply(pd.to_numeric, errors='ignore')
    df=df.select_dtypes('number')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 2018-08-20
      • 2018-05-08
      • 1970-01-01
      • 2016-01-29
      • 2019-11-14
      • 1970-01-01
      相关资源
      最近更新 更多