【发布时间】:2019-05-29 20:11:38
【问题描述】:
我已经使用 pyhive 从 hive 中提取数据到 python 中。现在我从导入的数据创建了一个数据框。数据框中所有列的 dtype 都是 object。我想知道删除数据框中包含字母(非数字)的任何列的最佳方法?
df.select_dtypes(['number']) 不起作用,因为我的所有列都是 dtype=object。
【问题讨论】:
我已经使用 pyhive 从 hive 中提取数据到 python 中。现在我从导入的数据创建了一个数据框。数据框中所有列的 dtype 都是 object。我想知道删除数据框中包含字母(非数字)的任何列的最佳方法?
df.select_dtypes(['number']) 不起作用,因为我的所有列都是 dtype=object。
【问题讨论】:
以下将数字字符串转换为数字 dtypes 并从 DataFrame 中仅选择数字 dtypes。
#convert number columns to numeric
df=df.apply(pd.to_numeric, errors='ignore')
df=df.select_dtypes('number')
【讨论】: