删除列值类型为string Pandas的行 [英] Remove rows where column value type is string Pandas

查看:188
本文介绍了删除列值类型为string Pandas的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框。我的一列应该只是浮标。当我尝试将该列转换为浮动时,我会收到警告,那里有字符串。我想删除此列中的值为字符串的所有行...

解决方案

使用 convert_objects 与param convert_numeric = True 这将强制任何非数值到 NaN

 在[24]中:

df = pd.DataFrame({'a' 0.5,'jasdh',9.0]})
df
出[24]:
a
0 0.1
1 0.5
2 jasdh
3 9
在[27]中:

df.convert_objects(convert_numeric = True)
输出[27]:
a
0 0.1
1 0.5
2 NaN
3 9.0
在[29]中:

然后可以删除它们:

  df.convert_objects(convert_numeric = True).dropna()
输出[ 29]:
a
0 0.1
1 0.5
3 9.0


I have a pandas dataframe. One of my columns should only be floats. When I try to convert that column to floats, I'm alerted that there are strings in there. I'd like to delete all rows where values in this column are strings...

解决方案

Use convert_objects with param convert_numeric=True this will coerce any non numeric values to NaN:

In [24]:

df = pd.DataFrame({'a': [0.1,0.5,'jasdh', 9.0]})
df
Out[24]:
       a
0    0.1
1    0.5
2  jasdh
3      9
In [27]:

df.convert_objects(convert_numeric=True)
Out[27]:
     a
0  0.1
1  0.5
2  NaN
3  9.0
In [29]:

You can then drop them:

df.convert_objects(convert_numeric=True).dropna()
Out[29]:
     a
0  0.1
1  0.5
3  9.0

这篇关于删除列值类型为string Pandas的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆