相关疑难解决方法(0)

熊猫:转换为数字,必要时创建NaN

假设我在数据框中有一个列,其中包含一些数字和一些非数字

>> df['foo']
0       0.0
1     103.8
2     751.1
3       0.0
4       0.0
5         -
6         -
7       0.0
8         -
9       0.0
Name: foo, Length: 9, dtype: object
Run Code Online (Sandbox Code Playgroud)

如何将此列转换为np.float,并将其他所有不浮动的列转换为NaN

当我尝试:

>> df['foo'].astype(np.float)
Run Code Online (Sandbox Code Playgroud)

要么

>> df['foo'].apply(np.float)
Run Code Online (Sandbox Code Playgroud)

我明白了 ValueError: could not convert string to float: -

python pandas

41
推荐指数
4
解决办法
7万
查看次数

标签 统计

pandas ×1

python ×1