[英]how to access rows of df depending on values of another column in another df
I have a df2
and a temp
df that has more rows and more columns(some are common) than df2.我有一个
df2
和一个temp
df,它比 df2 有更多的行和更多的列(有些是常见的)。
df2=pd.DataFrame(data=(['False','False',0.9],['False','True', 0.1],['True','False',0.2],['True','True',0.8]), columns=['Winter?','Sprinkler?', 'Rain?'] )
temp=pd.DataFrame(list(itertools.product([False, True], repeat=len(['Winter?','Sprinkler?', 'Rain?']))),columns=['Winter?','Sprinkler?', 'Rain?'])
temp["p"] = np.nan
I want to get the 'p'col values from df2 into temp df, in the rows with compatible values between the two (see screenshot below.)我想将 df2 中的 'p'col 值获取到 temp df 中,在两者之间具有兼容值的行中(请参见下面的屏幕截图。)
so expected output would be the following df:所以预计 output 将是以下df:
output_df = pd.DataFrame(list(itertools.product([False, True], repeat=len(['Winter?','Sprinkler?', 'Rain?']))),columns=['Winter?','Sprinkler?', 'Rain?'])
output_df["p"] = [0.9, 0.1, 0.9, 0.1, 0.2, 0.8, 0.2, 0.8]
This should not be as hard as I'm finding it, I understand that dataframes thrive for these kind of stuff but I have been stuck for quite some days so any help would be much这不应该像我发现的那么难,我知道数据框很适合这类东西,但我已经被困了好几天了,所以任何帮助都会很大
appreciated
赞赏
Use merge
:使用
merge
:
temp = temp.drop(columns='p').merge(df2, on=['Winter?', 'Rain?'], how='left')
print(temp)
# Output:
Winter? Sprinkler? Rain? p
0 False False False 0.9
1 False False True 0.1
2 False True False 0.9
3 False True True 0.1
4 True False False 0.2
5 True False True 0.8
6 True True False 0.2
7 True True True 0.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.