我有两个数据帧:
df1:
foo
0 2
1 11
2 18
3 6
4 14
5 12
6 8
7 13
8 7
9 5
df2:
bar date
0 2 06-01-2020
1 5 06-01-2020
2 7 06-01-2020
3 8 06-01-2020
4 3 06-01-2020
df1['result'] = df1.foo.isin(df2.bar)
如果df1的“foo”出现在df2的“bar”中,我想在df2中查找日期值。因此,我尝试了以下方法:
df1['date'] = df2['date'].loc[df1.foo.isin(df2.bar)]
但它给出了单列值的输出
输出:
foo result date
0 2 True 06-01-2020
1 11 False NaN
2 18 False NaN
3 6 False NaN
4 14 False NaN
5 12 False NaN
6 8 True NaN
7 13 False NaN
8 7 True NaN
9 5 True NaN
如果富谷不在酒吧,那么它应该有今天的日期,如下所示:
预期产出:
foo result date
0 2 True 06-01-2020
1 11 False 24-08-2020
2 18 False 24-08-2020
3 6 False 24-08-2020
4 14 False 24-08-2020
5 12 False 24-08-2020
6 8 True 06-01-2020
7 13 False 24-08-2020
8 7 True 06-01-2020
9 5 True 06-01-2020
您可以在熊猫中使用数据帧合并,如下所示:
使用合并
输出:
使用^{} 通过
df2
值创建的Series
添加日期时间,最后替换缺少的值实际日期时间:具有格式为
DD-MM-YYYY
的字符串DateTime的解决方案:如果使用日期时间:
相关问题 更多 >
编程相关推荐