英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论