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

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

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
import pandas as pd
table_one = pd.DataFrame({'Col1': [0.5, 1.0],
                          'Col2': [0.0, 0.5]},
                          index=['Sample1', 'Sample2'])
table_two = pd.DataFrame({'Col1': [0.0, 1.0],
                          'Col2': [1.0, 1.0]},
                          index=['Sample3', 'Sample4'])
table_result = pd.DataFrame({'Col1': [0.5, 2.0],
                          'Col2': [1.0, 1.5]},
                          index=[['Sample1','Sample3'], ['Sample2','Sample4']])
# 请在此处插入解决方案...
英文:

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
import pandas as pd
table_one = pd.DataFrame({'Col1': [0.5, 1.0],
                          'Col2': [0.0, 0.5]},
                          index=['Sample1', 'Sample2'])
table_two = pd.DataFrame({'Col1': [0.0, 1.0],
                          'Col2': [1.0, 1.0]},
                          index=['Sample3', 'Sample4'])
table_result = pd.DataFrame({'Col1': [0.5, 2.0],
                          'Col2': [1.0, 1.5]},
                          index=[['Sample1','Sample3'], ['Sample2','Sample4']])
# Please insert solution here...

答案1

得分: 2

我会使用以下代码:

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

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

from functools import reduce

tables = [table_one, table_two]
idx = list(zip(*(d.index for d in tables)))
out = reduce(lambda a, b: a.add(b), (d.set_axis(idx) for d in tables))

输出:

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

I would use:

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

Generalization to an arbitrary number of tables:

from functools import reduce

tables = [table_one, table_two]
idx = list(zip(*(d.index for d in tables)))
out = reduce(lambda a, b: a.add(b), (d.set_axis(idx) for d in tables))

Output:

                    Col1  Col2
(Sample1, Sample3)   0.5   1.0
(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:

确定