如何将两个pandas数据帧相加并保留两个索引。

huangapple go评论110阅读模式
英文:

How to add two pandas data frames and keep both indexes

问题

以下是翻译好的部分:

问题: 什么是添加两个 Pandas 数据框并保持两者索引的最优雅和高效的方法?

表格 1

索引 Col1 Col2
Sample1 0.5 1.0
Sample2 0.0 0.5

表格 2

索引 Col1 Col2
Sample3 0.0 1.0
Sample4 1.0 1.0

结果表格

索引 Col1 Col2
[Sample1, Sample3] 0.5 2.0
[Sample2, Sample4] 1.0 1.5
  1. import pandas as pd
  2. table_one = pd.DataFrame({'Col1': [0.5, 1.0],
  3. 'Col2': [0.0, 0.5]},
  4. index=['Sample1', 'Sample2'])
  5. table_two = pd.DataFrame({'Col1': [0.0, 1.0],
  6. 'Col2': [1.0, 1.0]},
  7. index=['Sample3', 'Sample4'])
  8. table_result = pd.DataFrame({'Col1': [0.5, 2.0],
  9. 'Col2': [1.0, 1.5]},
  10. index=[['Sample1','Sample3'], ['Sample2','Sample4']])
  11. # 请在此处插入解决方案...
英文:

What is the most elegant and efficient way of adding two pandas data frames and keep both indexes?

Table 1

Index Col1 Col2
Sample1 0.5 1.0
Sample2 0.0 0.5

Table 2

Index Col1 Col2
Sample3 0.0 1.0
Sample4 1.0 1.0

Result Table

Index Col1 Col2
[Sample1, Sample3] 0.5 2.0
[Sample2, Sample4] 1.0 1.5
  1. import pandas as pd
  2. table_one = pd.DataFrame({'Col1': [0.5, 1.0],
  3. 'Col2': [0.0, 0.5]},
  4. index=['Sample1', 'Sample2'])
  5. table_two = pd.DataFrame({'Col1': [0.0, 1.0],
  6. 'Col2': [1.0, 1.0]},
  7. index=['Sample3', 'Sample4'])
  8. table_result = pd.DataFrame({'Col1': [0.5, 2.0],
  9. 'Col2': [1.0, 1.5]},
  10. index=[['Sample1','Sample3'], ['Sample2','Sample4']])
  11. # Please insert solution here...

答案1

得分: 2

我会使用以下代码:

  1. out = table_one.add(table_two.set_axis(table_one.index))
  2. out.index = zip(table_one.index, table_two.index)

对于任意数量的表格泛化:

  1. from functools import reduce
  2. tables = [table_one, table_two]
  3. idx = list(zip(*(d.index for d in tables)))
  4. out = reduce(lambda a, b: a.add(b), (d.set_axis(idx) for d in tables))

输出:

  1. Col1 Col2
  2. (Sample1, Sample3) 0.5 1.0
  3. (Sample2, Sample4) 2.0 1.5
英文:

I would use:

  1. out = table_one.add(table_two.set_axis(table_one.index))
  2. out.index = zip(table_one.index, table_two.index)

Generalization to an arbitrary number of tables:

  1. from functools import reduce
  2. tables = [table_one, table_two]
  3. idx = list(zip(*(d.index for d in tables)))
  4. out = reduce(lambda a, b: a.add(b), (d.set_axis(idx) for d in tables))

Output:

  1. Col1 Col2
  2. (Sample1, Sample3) 0.5 1.0
  3. (Sample2, Sample4) 2.0 1.5

huangapple
  • 本文由 发表于 2023年7月20日 19:25:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定