[英]Join pandas dataframes according to common index value only
我有以下数据帧(这只是测试数据),在实际样本中,我有在 dataframe 1 和 dataframe 2 中重复几次的索引值 - 这导致最终 dataframe 中的重复/重复行。
DataFrame 1:
pd.DataFrame({'id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10},
'first_name': {0: 'Jennee',
1: 'Dagny',
2: 'Correy',
3: 'Pall',
4: 'Julie',
5: 'Janene',
6: 'Lemmy',
7: 'Coleman',
8: 'Beck',
9: 'Che'},
'last_name': {0: 'Strelitzki',
1: 'Dunsire',
2: 'Wickrath',
3: 'Jopp',
4: 'Gheeraert',
5: 'Gawith',
6: 'Farrow',
7: 'Legging',
8: 'Beckwith',
9: 'Burgoin'},
'email': {0: 'jstrelitzki0@google.de',
1: 'ddunsire1@geocities.com',
2: 'cwickrath2@github.com',
3: 'pjopp3@infoseek.co.jp',
4: 'jgheeraert4@theatlantic.com',
5: 'jgawith5@sciencedirect.com',
6: 'lfarrow6@wikimedia.org',
7: 'clegging7@businessinsider.com',
8: 'bbeckwith8@zdnet.com',
9: 'cburgoin9@reference.com'},
'gender': {0: 'Male',
1: 'Female',
2: 'Female',
3: 'Female',
4: 'Female',
5: 'Female',
6: 'Male',
7: 'Female',
8: 'Polygender',
9: 'Male'},
'ip_address': {0: '8.99.68.120',
1: '188.238.129.48',
2: '87.159.243.249',
3: '66.37.174.94',
4: '233.77.128.104',
5: '190.202.131.98',
6: '84.175.231.196',
7: '140.178.100.5',
8: '81.211.179.167',
9: '31.219.69.206'},
'Boolean': {0: False,
1: False,
2: True,
3: True,
4: False,
5: True,
6: True,
7: False,
8: False,
9: False}})
DataFrame 2:
pd.DataFrame({'id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10},
'Model': {0: 2005,
1: 2007,
2: 2011,
3: 2003,
4: 1998,
5: 1992,
6: 1992,
7: 1992,
8: 2008,
9: 1996},
'Make': {0: 'Cadillac',
1: 'Lexus',
2: 'Dodge',
3: 'Dodge',
4: 'Oldsmobile',
5: 'Volkswagen',
6: 'Chevrolet',
7: 'Suzuki',
8: 'Ford',
9: 'Mazda'},
'Colour': {0: 'Red',
1: 'Red',
2: 'Crimson',
3: 'Red',
4: 'Purple',
5: 'Crimson',
6: 'Red',
7: 'Aquamarine',
8: 'Puce',
9: 'Maroon'}})
这两个数据帧应该仅基于在两个数据帧中找到的公共索引值进行连接。 这意味着,任何在这两个数据框中不匹配的索引值; 不应出现在最终合并/合并的 dataframe 中。
我想确保最终的 dataframe 是唯一的,并且只捕获基于唯一索引值的列组合。
当我尝试使用以下代码时,output 应该根据在两个数据帧中找到的唯一索引进行“内部连接”。
final = pd.merge(df1, df2, left_index=True, right_index=True)
但是,当我尝试在较大的(其他)pandas 数据帧上应用上述合并技术时,有许多行被重复/复制了多次。 当使用更多数据帧合并几次时,行会非常频繁地重复,并具有相同的索引值。
我期望看到每行返回一个索引值(每个数据帧的所有列组合)。
我不确定为什么会这样。 我可以确认数据集没有问题。
是否有更好的技术来合并这两个数据帧,仅基于公共索引值,同时确保我不会在我的最终 dataframe 中重复任何行(具有相同的索引)? 我经常发现这种合并通常也会创建一个大小约为 20GB 的巨大最终 CSV 文件。 源文件总共只有 15MB 左右。
任何帮助深表感谢。
我的终端 output 应该是这样的(请复制并用作 Pandas DF):
pd.DataFrame({'id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10},
'first_name': {0: 'Jennee',
1: 'Dagny',
2: 'Correy',
3: 'Pall',
4: 'Julie',
5: 'Janene',
6: 'Lemmy',
7: 'Coleman',
8: 'Beck',
9: 'Che'},
'last_name': {0: 'Strelitzki',
1: 'Dunsire',
2: 'Wickrath',
3: 'Jopp',
4: 'Gheeraert',
5: 'Gawith',
6: 'Farrow',
7: 'Legging',
8: 'Beckwith',
9: 'Burgoin'},
'email': {0: 'jstrelitzki0@google.de',
1: 'ddunsire1@geocities.com',
2: 'cwickrath2@github.com',
3: 'pjopp3@infoseek.co.jp',
4: 'jgheeraert4@theatlantic.com',
5: 'jgawith5@sciencedirect.com',
6: 'lfarrow6@wikimedia.org',
7: 'clegging7@businessinsider.com',
8: 'bbeckwith8@zdnet.com',
9: 'cburgoin9@reference.com'},
'gender': {0: 'Male',
1: 'Female',
2: 'Female',
3: 'Female',
4: 'Female',
5: 'Female',
6: 'Male',
7: 'Female',
8: 'Polygender',
9: 'Male'},
'ip_address': {0: '8.99.68.120',
1: '188.238.129.48',
2: '87.159.243.249',
3: '66.37.174.94',
4: '233.77.128.104',
5: '190.202.131.98',
6: '84.175.231.196',
7: '140.178.100.5',
8: '81.211.179.167',
9: '31.219.69.206'},
'Boolean': {0: False,
1: False,
2: True,
3: True,
4: False,
5: True,
6: True,
7: False,
8: False,
9: False},
'Model': {0: 2005,
1: 2007,
2: 2011,
3: 2003,
4: 1998,
5: 1992,
6: 1992,
7: 1992,
8: 2008,
9: 1996},
'Make': {0: 'Cadillac',
1: 'Lexus',
2: 'Dodge',
3: 'Dodge',
4: 'Oldsmobile',
5: 'Volkswagen',
6: 'Chevrolet',
7: 'Suzuki',
8: 'Ford',
9: 'Mazda'},
'Colour': {0: 'Red',
1: 'Red',
2: 'Crimson',
3: 'Red',
4: 'Purple',
5: 'Crimson',
6: 'Red',
7: 'Aquamarine',
8: 'Puce',
9: 'Maroon'}})
这是具有非唯一 idx 值的预期行为。 由于一个 df 中有 3 个 ID1 行,另一个中有 2 个 ID1 行,因此合并后的 df 中最终有 6 个 ID1 行。 如果您将validate="one_to_one"
添加到 pd.merge() 您将收到此错误。 MergeError:合并键在左右数据集中都不唯一; 不是一对一合并,除多对多外,所有其他验证均失败。
如果它对您的数据有意义,您可以使用 left_on 和 right_on 参数来查找独特的组合,并在您需要时为您提供一对一组合。
在您的新数据后编辑:
现在您有了唯一的 ID,这应该对您有用。 请注意,它不会引发验证错误。
final = pd.merge(df1, df2, left_on=['id'], right_on=['id'], validate='one_to_one')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.