python - 有没有基于索引和列的 Pandas 方法?

标签 python pandas join

我试图通过从另一个 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/

相关文章:

python - 如何用 pandas DataFrame 中之前和后续值的平均值替换 NaN?

mysql - 根据表 1 上的每个 id 连接 2 个表

php - mysql 用多个表搜索电子邮件和域名

Python:如何删除特定列为空/NaN 的行?

python - Pandas 从系列中获取月末值

python - 无法删除Python3.5

python - "Tuple comprehensions"和 star splat/unpack 运算符 *

MySQL Join 忽略电子邮件列值为空的行

python - 在 Python 中将 int 赋值给变量是什么意思?

python - 内存泄漏(?)Python 3.2