简体   繁体   English

根据条件替换熊猫数据框列中的数据,如果不满足条件则跳过

[英]Replacing data in a pandas dataframe column based on condition, skipping if condition not met

I have a dataframe with a column of numbers. 我有一个带有数字列的数据框。 If the number is less than 0, I would like to add 3.14 to that number. 如果数字小于0,我想在该数字上加上3.14。 If not, I would like to skip that number and leave it as is. 如果没有,我想跳过该数字并保持原样。 I am using pandas and numpy but cannot find out how to just skip the numbers not meeting the condition while leaving them in the dataframe as is. 我正在使用pandas和numpy,但无法找出如何跳过不满足条件的数字,而将它们直接保留在数据框中。

When trying this: 尝试此操作时:

df['rad'].apply(lambda x: [y if y>=0 else y+3.14 for y in x])

I get TypeError: 'int' object is not iterable 我收到TypeError:“ int”对象不可迭代

Even though the numbers in 'rad' column are all int64. 即使“ rad”列中的数字均为int64。

那是np.where

np.where(df['rad']>0,df['rad'],df['rad']+3.14)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM