我有两个df,他们需要合并在两个键通道和周。当尝试合并时,它会返回Contacts_F列中所有行的NaN值。 日期:
df = pd.DataFrame({ 'week' : ['01', '02', '45'] ,
'Channel' : ['AA', 'BB', 'CC'],
'level' : ['1degre','2degre','3degre'] })
df.dtypes
out[131]:
week object
Channel object
level object
reff_table = pd.DataFrame({ 'week' : ['01', '02', '45'] ,
'Channel' : ['AA', 'BB', 'CC'],
'Contacts_F' : ['0.11','0.546','0.06458'] })
reff_table.dtypes
out[132]:
week object
Channel object
Contacts_F float64
print(df.week[:3]), print(reff_table.week[:3])
0 00
1 00
2 00
Name: week, dtype: object
0 103
1 102
2 101
Name: week, dtype: object
(None, None)
我的尝试: /.
df = pd.merge(df, reff_table [["Channel", "week" , "Contacts_F"]],on=['Channel','week'], how='left')
如果我只在Column=Channel上合并,它会工作,但是我需要匹配两列。
df = pd.merge(df, reff_table [["Channel", "week" , "Contacts_F"]],on=['Channel', how='left')
问题来源StackOverflow 地址:/questions/59383082/merge-df-in-two-keys-just-working-for-one-key
有关如何执行合并的工作示例,请参见下面。各种语法错误的提示被纠正。
import pandas as pd
df = pd.DataFrame({'week': [1, 2, 45],
'Channel': ['AA', 'BB', 'CC'],
'level': ['1degre', '2degre', '3degre']})
reff_table = pd.DataFrame({'week': [1, 2, 45],
'Channel': ['AA', 'BB', 'CC'],
'Contacts_F': ['0.11', '0.546', '0.06458']})
pd.merge(df, reff_table, on=['Channel', 'week'], how='left')
------
Out[8]:
week Channel level Contacts_F
0 1 AA 1degre 0.11
1 2 BB 2degre 0.546
2 45 CC 3degre 0.06458
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。