简体   繁体   English

如果 Nan 在单独的行中,则将值替换为 Nan - Pandas

[英]Replace value with Nan if Nan in separate row - Pandas

I have a column in a df that contains numerous strings.我在df中有一个包含许多字符串的列。 I was to replace some of the strings with Nan but there's too many to list that can be used to replace these values.我打算用Nan replace一些字符串,但是列出的字符串太多了,可以用来replace这些值。 I have a separate column that does contain Nan values, which may be used to achieve this.我有一个单独的列,其中包含Nan值,可用于实现此目的。

I want to replace specific strings in Value .我想replace Value中的特定字符串。 Just imagine if this column contains 1000 different strings and I want to replace 500 with Nan .想象一下,如果这个列包含 1000 个不同的字符串,我想用Nan replace 500 。 it would be inefficient to create a list with these unwanted strings and use it to replace with Nan用这些不需要的字符串创建一个列表并用它来替换Nan效率低下

There is a separate column (X) that displays Nan values that can be used to replace rows in Value .有一个单独的列(X)显示可用于replace Value 中的行的Nan Value So where X is Nan , replace the row in Value with Nan .因此,如果XNan ,请将Value中的行替换为Nan

Is there an easier way to do this?有没有更简单的方法来做到这一点?

df = pd.DataFrame({        
    'Value' : ['B','A','X','Y','C','D','E','F','G','H','I'],
    'X' : ['A','A','A','A',np.nan,'A','A','A',np.nan,'A','A'],  
    })

df = df.loc[df['X'].eq(np.nan), df['Value']] = np.nan
print(df)

Intended Output:预期 Output:

   Value    X
0      B    A
1      A    A
2      X    A
3      Y    A
4      Nan  NaN
5      D    A
6      E    A
7      F    A
8      NaN  NaN
9      H    A
10     I    A

You want DataFrame.mask with Series.isna你想要DataFrame.maskSeries.isna

df=df.mask(df['X'].isna())
print(df)

   Value    X
0      B    A
1      A    A
2      X    A
3      Y    A
4    NaN  NaN
5      D    A
6      E    A
7      F    A
8    NaN  NaN
9      H    A
10     I    A

Also you can use DataFrame.where with Series.notna您也可以将DataFrame.whereSeries.notna一起使用

df=df.where(df['X'].notna())

We can do dropna + reindex我们可以做dropna + reindex

df=df.dropna().reindex(df.index)
   Value    X
0      B    A
1      A    A
2      X    A
3      Y    A
4    NaN  NaN
5      D    A
6      E    A
7      F    A
8    NaN  NaN
9      H    A
10     I    A

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

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