python - 如何在 Pandas Python 中更新数据框

标签 python pandas dataframe

我在 pandas 中有以下两个数据框:

DF1:
AuthorID1  AuthorID2  Co-Authored
A1         A2         0
A1         A3         0
A1         A4         0
A2         A3         0

DF2:
AuthorID1  AuthorID2  Co-Authored
A1         A2         5
A2         A3         6
A6         A7         9

我希望(无需循环和比较)找到 DF2 中存在于 DF1 中的匹配 AuthorID1 和 AuthorID2 配对,并相应地更新列值。所以上面两个表的结果如下:

Resulting Updated DF1:
AuthorID1  AuthorID2  Co-Authored
A1         A2         5
A1         A3         0
A1         A4         0
A2         A3         6

有没有快速的方法来做到这一点?因为我在 DF1 中有 700 万行,循环和比较将花费很长时间。

更新:请注意,DF2 中的最后两个不应成为 DF1 中更新的一部分,因为它在 DF1 中不存在

最佳答案

您可以使用update :

df1.update(df2)
print (df1)
  AuthorID1 AuthorID2  Co-Authored
0        A1        A2          5.0
1        A2        A3          6.0
2        A1        A4          0.0
3        A2        A3          0.0

示例:

df1 = pd.DataFrame({'new': {0: 7, 1: 8, 2: 1, 3: 3}, 
                    'AuthorID2': {0: 'A2', 1: 'A3', 2: 'A4', 3: 'A3'}, 
                    'AuthorID1': {0: 'A1', 1: 'A1', 2: 'A1', 3: 'A2'}, 
                    'Co-Authored': {0: 0, 1: 0, 2: 0, 3: 0}})

df2 = pd.DataFrame({'AuthorID2': {0: 'A2', 1: 'A3'},
                    'AuthorID1': {0: 'A1', 1: 'A2'}, 
                    'Co-Authored': {0: 5, 1: 6}})

  AuthorID1 AuthorID2  Co-Authored  new
0        A1        A2            0    7
1        A1        A3            0    8
2        A1        A4            0    1
3        A2        A3            0    3

print (df2)
  AuthorID1 AuthorID2  Co-Authored
0        A1        A2            5
1        A2        A3            6

df1.update(df2)
print (df1)
  AuthorID1 AuthorID2  Co-Authored  new
0        A1        A2          5.0    7
1        A2        A3          6.0    8
2        A1        A4          0.0    1
3        A2        A3          0.0    3

按评论编辑:

我认为您需要首先使用 isindf1 过滤 df2 :

df2 = df2[df2[['AuthorID1','AuthorID2']].isin(df1[['AuthorID1','AuthorID2']]).any(1)]
print (df2)
  AuthorID1 AuthorID2  Co-Authored
0        A1        A2            5
1        A2        A3            6

df1.update(df2)
print (df1)
  AuthorID1 AuthorID2  Co-Authored
0        A1        A2          5.0
1        A2        A3          6.0
2        A1        A4          0.0
3        A2        A3          0.0

关于python - 如何在 Pandas Python 中更新数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38827835/

相关文章:

python - 如何从 Python 调用的 matlab 函数中传递字典作为参数

python - 如何迭代 DataFrame 并生成新的 DataFrame

python - 属性错误 : 'module' object has no attribute 'strptime' -- Possible Bug?

r - 从数据框中提取带条件的列名

dataframe - KDB+ 如何检查所有列是否具有相同的符号?

python - Pandas:重组数据框

python - Pandas DataFrame 包含 JSON 格式的日期和时间

python - 在 Pandas groupby 上使用 value_counts 时如何忽略空系列?

python - 如何按条件删除列?

python - 如何将具有多个聚合字段和多个索引字段的 pandas 数据框转置为 python 中的 sumIfs?