[英]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
.因此,如果
X
是Nan
,请将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.mask
和Series.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.where
与Series.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.