[英]how to modify a df based on another df boolean values?
I got 2 DataFrames. 我有2个DataFrames。 df1 (booleans) and df2 (integers).
df1(布尔值)和df2(整数)。 I can't figure out how to modify df2 based on df1.
我不知道如何基于df1修改df2。
Goal: To modify df2 and change values to 100 if False in df1. 目标:修改df2并将df1中的False值更改为100。 If True don't change.
如果为True,请不要更改。
df1
date x y w z
2017-02-02 True False True True
2017-02-03 False True True True
df2
date x y w z
2017-02-02 1 2 3 4
2017-02-03 2 4 1 3
Expectation 期望
df2
date x y w z
2017-02-02 1 100 3 4
2017-02-03 100 4 1 3
这可以通过一个简单的条件语句来实现
df2[df1==False] = 100
where
+ iloc
where
+ iloc
You can use pd.DataFrame.where
, ensuring you choose only relevant columns (ie ignoring the first columns). 您可以使用
pd.DataFrame.where
,确保仅选择相关列(即忽略前pd.DataFrame.where
列)。 There's usually never a need to explicitly check == False
or == True
with Pandas. 还有平时从不需要明确检查
== False
或== True
有大熊猫。
df2.iloc[:, 1:] = df2.iloc[:, 1:].where(df1.iloc[:, 1:], 100)
print(df2)
date x y w z
0 2017-02-02 1 100 3 4
1 2017-02-03 100 4 1 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.