我试图通过从另一个 DataFrame 中提取值来在 Pandas DataFrame 中创建一个新列。对于每个索引,它应该使用与现有 DataFrames 值匹配的列值。这是一个可行的解决方案,但我正在寻找使用 Pandas 实现此目的的最佳方法。
import pandas as pd
dates = pd.date_range('2020-01-01', '2020-01-03', freq='d')
A = pd.DataFrame({
'i': [1,2,3],
}, index=dates)
B = pd.DataFrame({
1: [11, 12, 13],
2: [21, 22, 23],
3: [31, 32, 33],
}, index=dates)
# replace this with a more efficient method, avoiding for-loop and creating C
r = [B.loc[k, v] for k, v in A.i.items()]
C = pd.DataFrame({'B': r}, index=dates)
pd.merge(A, C, left_index=True, right_index=True)
预期结果:
i B
2020-01-01 1 11
2020-01-02 2 22
2020-01-03 3 33
最佳答案
如果我理解正确的话:
# Reshape main df with index as (date, i), remove axis name
_A = A.set_index('i', append=True).rename_axis(index=lambda x: None)
# Reshape sub df with index as (date, i), name series (column) as 'B'
_B = B.stack().rename('B')
# perform left join on indices
_A.merge(_B, how='left', left_index=True, right_index=True)
结果:
B
2020-01-01 1 11
2020-01-02 2 22
2020-01-03 3 33
你可以将整个集合串成一行,但我不推荐这个怪物:
A.set_index('i', append=True).rename_axis(index=lambda x: None) \
.merge(B.stack().rename('B'), how='left', left_index=True, right_index=True)
关于python - 有没有基于索引和列的 Pandas 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62414703/