繁体   English   中英

Python、Pandas:比较两个数据帧并返回组合

[英]Python, Pandas: Compare two dataframes and return combined

晚上好,

我想知道,比较两个数据帧并返回它们的组合的最佳方法是什么? 或者,如果 pandas 内部甚至有内置 function?

例如,这是我的两个数据框:

Dataframe 01:

first_name | age | id | value_a | value_b | value_c
peter      | 37  | 19 | 4562    | 78      | 21.5
jane       | 32  | 5  | 3832    | 85      | 17.0
michael    | 43  | 41 | 2195    | 63      | 44.4

Dataframe 02:

first_name | age | id | value_a | value_b | value_c
sarah      | 51  | 2  | 63      | 81      | 4.1
peter      | 37  | 19 | 4562    | 81      | 21.5
tom        | 22  | 89 | 107     | 14      | 0.0
michael    | 43  | 41 | 1838    | 63      | 44.4

如您所见,整个 dataframe(Dataframe 02)中有一些新条目,并且还列出了一些已经存在的条目 --> 在这些行中进行了一些更改? 我想要实现的是一个 new(,) dataframe 包含所有新行:已经存在的行和更新的行! 在这种情况下:

Dataframe 新

first_name | age | id | value_a | value_b | value_c
peter      | 37  | 19 | 4562    | 81      | 21.5
jane       | 32  | 5  | 3832    | 85      | 17.0
michael    | 43  | 41 | 1838    | 63      | 44.4
sarah      | 51  | 2  | 63      | 81      | 4.1
tom        | 22  | 89 | 107     | 14      | 0.0

笔记:

  • 总有一个列(这里:'id')可以看作是一个不变的键
  • 行数可能不同
  • 列的数量和名称始终保持不变
  • 行的顺序并不重要

感谢您的所有帮助和美好的夜晚!

既然您问pandas 内部是否还有内置的 function? . 答案是肯定的,在 pandas 中有一个内置的 function 允许您比较相同标记(具有相同索引和列)的数据帧。

There is a DataFrame.compare function which is available in pandas version >= 1.1.0 and allows you to compare first dataframe to second DataFrame and show the differences:

所以,现在让我们看看你所说的例子

  • 总有一个列(这里:'id')可以看作是一个不变的键
  • 列的数量和名称始终保持不变

因此,为了比较两个数据帧,您首先需要align index这可以使用DataFrame.align id来完成

d1, d2 = df1.set_index('id').align(df2.set_index('id'))

现在您可以在对齐的数据帧上使用DataFrame.compare

d1.compare(d2, keep_equal=True)

结果:

         first_name     age           value_a         value_b       value_c      
         self    other  self other    self   other    self other    self other
id                                                                            
2         NaN    sarah   NaN  51.0     NaN    63.0     NaN  81.0     NaN   4.1
5        jane      NaN  32.0   NaN  3832.0     NaN    85.0   NaN    17.0   NaN
19      peter    peter  37.0  37.0  4562.0  4562.0    78.0  81.0    21.5  21.5
41    michael  michael  43.0  43.0  2195.0  1838.0    63.0  63.0    44.4  44.4
89        NaN      tom   NaN  22.0     NaN   107.0     NaN  14.0     NaN   0.0

现在来回答你的第二个问题:

如何实现一个新的(?)dataframe,其中包含所有新行,已经存在的行和更新的行!

您可以在对齐的数据帧d1d2上使用DataFrame.comine_first

d2.combine_first(d1)

或者,在未对齐的情况下,如下所示:

df2.set_index('id').combine_first(df1.set_index('id'))

结果:

   first_name   age  value_a  value_b  value_c
id                                            
2       sarah  51.0     63.0     81.0      4.1
5        jane  32.0   3832.0     85.0     17.0
19      peter  37.0   4562.0     81.0     21.5
41    michael  43.0   1838.0     63.0     44.4
89        tom  22.0    107.0     14.0      0.0

这是一种方法

>>> (pd.concat([df1, df2])
       .drop_duplicates(subset=['id','first_name'], keep='last')
       .reset_index(drop=True)
       .set_index('first_name')
     )

first_name  |  age   |  id   |   value_a  |  value_b   |   value_c
jane        |   32   |   5   |     3832   |       85   |     17.0
sarah       |   51   |   2   |       63   |       81   |      4.1
peter       |   37   |  19   |     4562   |       81   |     21.5
tom         |   22   |  89   |      107   |       14   |      0.0
michael     |   43   |  41   |     1838   |       63   |     44.4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM