gpt4 book ai didi

Pandas 合并指标自定义值

转载 作者:行者123 更新时间:2023-12-05 01:41:28 25 4
gpt4 key购买 nike

在 pandas 合并期间将指示器更新为更友好消息的最快方法是什么?默认的 indicator= True 产生 left_only,right_only,both,我想更新为 Only出现在上个月的数据中只出现在当月的数据中出现在两个月的数据中

我希望在没有 lambda 运算符 的情况下做到这一点。

最佳答案

创建一个工作示例:

np.random.seed(0)
left = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': np.random.randn(4)})
right = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': np.random.randn(4)})

merged=left.merge(right,on='key',how='outer',indicator=True)
print(merged)

key value_x value_y _merge
0 A 1.764052 NaN left_only
1 B 0.400157 1.867558 both
2 C 0.978738 NaN left_only
3 D 2.240893 -0.977278 both
4 E NaN 0.950088 right_only
5 F NaN -0.151357 right_only

用于映射值:

d={"left_only":"Only present in last month's data", "right_only":"Only present in current month's data","both":"Present in Both month's data"}

merged['_merge'] = merged['_merge'].map(d)
print(merged)

key value_x value_y _merge
0 A 1.764052 NaN Only present in last month's data
1 B 0.400157 1.867558 Present in Both month's data
2 C 0.978738 NaN Only present in last month's data
3 D 2.240893 -0.977278 Present in Both month's data
4 E NaN 0.950088 Only present in current month's data
5 F NaN -0.151357 Only present in current month's data

关于Pandas 合并指标自定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54351136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com