简体   繁体   English

替换熊猫数据框列中的当前值(NA / NaN除外)

[英]Replacing present values (all except NA/NaN) in pandas data frame column

There is a fillna method for filling missing values, but is there a method for replacing all the actual values with a given value. 有一个fillna方法可以填充缺失值,但是有一种方法可以用给定值替换所有实际值。

For example: 例如:

      c
  0  NA/NaN
  1  2.0
  2  NA/NaN
  3  6.0
  4  8.0
  5  NA/NaN
  6  12.0

For every data point, I would like to mark it with 'v' meaning that it contains a valid value: 对于每个数据点,我想用'v'标记它意味着它包含一个有效值:

      c
  0   NA/NaN
  1  'v'
  2   NA/NaN
  3  'v'
  4  'v'
  5   NA/NaN
  6  'v'
  • Is there a specific reason why you want to overwrite the existing value, instead of keeping a separate series or column of validity ? 您是否有特定原因想要覆盖现有值,而不是保留单独的validity序列或列?
  • And using a custom marker instead of a boolean value is also an extra complexity. 使用自定义标记而不是boolean值也是一种额外的复杂性。

Further down in your code it would be easier to simply us 在您的代码中,进一步简化我们

valid = df['c'].notnull()

if you really want to overwrite it with a marker string: 如果您真的想用标记字符串覆盖它:

df.loc[df['c'].notnull(), 'c'] = 'v'

试试这个:

df.loc[~df['c'].isnull()] = "'v'"

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

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