[英]Change negative values in a column containing nan in python dataframe?
hi I have a dataframe like this, I want to replace negtive values with 0, but at the same time keep Nan values.嗨,我有一个这样的 dataframe,我想用 0 替换负值,但同时保留 Nan 值。 The below code doesn't work because df['Data']<0
can't supported between instances of 'str' and 'int'.下面的代码不起作用,因为df['Data']<0
在 'str' 和 'int' 的实例之间不被支持。 Any simple suggetions?有什么简单的建议吗?
df[(df['Data'].notnull())& (df['Data']<0)]
Data
0 1
1 0.5
2 0
3 -0.5
4 -1
5 Nan
6 Nan
wanted result想要的结果
Data
0 1
1 0.5
2 0
3 0
4 0
5 Nan
6 Nan
to replace numbers less than 0 by 0, while keeping NaN as is, you can use loc
and equate it to 0. Code here将小于 0 的数字替换为 0,同时保持 NaN 不变,您可以使用loc
并将其等同于 0。代码在这里
data1 = {'Data': [1, 0.5, -0.5, 0, -1, np.nan, np.nan]}
df=pd.DataFrame(data1)
>>df
Data
0 1.0
1 0.5
2 -0.5
3 0.0
4 -1.0
5 NaN
6 NaN
df.loc[df['Data']<0,'Data'] = 0
>>df
Data
0 1.0
1 0.5
2 0.0
3 0.0
4 0.0
5 NaN
6 NaN
Going by your error message - it looks like your Data
column has an object
dtype - you can get around it by converting it to float
根据您的错误消息 - 看起来您的Data
列有一个object
- 您可以通过将其转换为float
来绕过它
>>> x = [1, 0.5, 0, -0.5, -1, 'nan', 'Nan']
>>> df = pd.DataFrame(x, columns=['Data'])
This gives me the same error you describe -这给了我你描述的同样的错误 -
>>> df[(df['Data'].notnull())& (df['Data']<0)]
TypeError: '<' not supported between instances of 'str' and 'int'
But this replaces the negative numbers while keeping the nan
intact但这取代了负数,同时保持nan
完好无损
>>> df.loc[(df['Data'].astype(float).notnull())& (df['Data'].astype(float)<0), ['Data']] = 0
>>> df
Data
0 1
1 0.5
2 0
3 0
4 0
5 nan
6 Nan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.