【问题标题】:How to compare columns from two different Data Frames and keep the values from the first Data Frame?如何比较两个不同数据框中的列并保留第一个数据框中的值?
【发布时间】:2021-01-26 07:54:00
【问题描述】:

我有两个不同大小的数据框。它们都有四列:单词、x、y 和 z。

但是,在加入这两个数据框时,我想保留相似词的 x、y、z 值。 df1中不存在但df2中存在的单词被保留。

我尝试使用pd.merge,但这将保留两个值并且只保留相似的单词。如果我使用 pd.concat 我必须删除类似的元素,但不会来自第一个数据帧。

示例

df1 = pd.DataFrame({'Words': 
       ['aardvark', 'abalone', 'abandon'],
     'x': [0.999, 0.888, 0.777], 
     'y': [0.999, 0.888, 0.777],
     'z': [0.999, 0.888, 0.777]})

df2 = pd.DataFrame({'Words': 
       ['aaaaahh', 'aardvark', 'abalone', 'abandon', 'zoo', 'zoom', 'zucchini'], 
     'x': [0.199, 0.111, 0.222, 0.333, 0.232, 0.842, 0.945], 
     'y': [0.929, 0.111, 0.222, 0.333, 0.112, 0.62, 0.265],
     'z': [0.993, 0.111, 0.222, 0.333, 0.212, 0.344, 0.745]})

# Expected output
df_res = pd.DataFrame({'Words': 
          ['aaaaahh', 'aardvark', 'abalone', 'abandon', 'zoo', 'zoom', 'zucchini'], 
     'x': [0.199, 0.999, 0.888, 0.777, 0.232, 0.842, 0.945], 
     'y': [0.929, 0.999, 0.888, 0.777, 0.112, 0.62, 0.265],
     'z': [0.993, 0.999, 0.888, 0.777, 0.212, 0.344, 0.745]})

我尝试了什么

import pandas as pd

# Merge
df_res = pd.merge(df1, df2, on='Word', how='inner')

# Concat
df_concat = pd.concat(objs=[df1, df2], ignore_index=True)
df_concat = pd.drop_duplicates(subset=['Word'], keep=False, ignore_index=True)

# Compare
d_res = d1[(d1['Word'] != d1['Word'])]
ValueError: Can only compare identically-labeled Series objects

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可以使用df.append 将df1 附加到df2,然后是drop_duplicates,然后是keep='last',然后是sort_index 和reset_index:

    >>> (df2.append(df1)
            .drop_duplicates('Words', keep='last')
            .sort_index()
            .reset_index(drop=True))
    
          Words      x      y      z
    0   aaaaahh  0.199  0.929  0.993
    1  aardvark  0.999  0.999  0.999
    2   abalone  0.888  0.888  0.888
    3   abandon  0.777  0.777  0.777
    4       zoo  0.232  0.112  0.212
    5      zoom  0.842  0.620  0.344
    6  zucchini  0.945  0.265  0.745
    

    【讨论】:

      【解决方案2】:

      也许性能不如@Sayandip Dutta 的回答,您可以尝试右连接(或左连接,具体取决于您在 pd.merge 中放置参数的顺序):

      In [4]: res = pd.merge(df1, df2, how='right', on='Words', suffixes=("_1", "_2"))
      
      In [5]: res
      Out[6]:
            Words    x_1    y_1    z_1    x_2    y_2    z_2
      0  aardvark  0.999  0.999  0.999  0.111  0.111  0.111
      1   abalone  0.888  0.888  0.888  0.222  0.222  0.222
      2   abandon  0.777  0.777  0.777  0.333  0.333  0.333
      3   aaaaahh    NaN    NaN    NaN  0.199  0.929  0.993
      4       zoo    NaN    NaN    NaN  0.232  0.112  0.212
      5      zoom    NaN    NaN    NaN  0.842  0.620  0.344
      6  zucchini    NaN    NaN    NaN  0.945  0.265  0.745
      

      然后你可以fillna x_1, y_1, z_1 的值为 x_2, y_2 和 z_2。

      In [8]: res.x_1.fillna(res.x_2, inplace=True)
      
      In [8]: res.y_1.fillna(res.y_2, inplace=True)
      
      In [9]: res.z_1.fillna(res.z_2, inplace=True)
      
      In [10]: df_res = res[["Words", "x_1", "y_1", ,"z_1"]]
      
      In [11]: df_res
      Out[11]:
            Words    x_1    y_1    z_1
      0  aardvark  0.999  0.999  0.999
      1   abalone  0.888  0.888  0.888
      2   abandon  0.777  0.777  0.777
      3   aaaaahh  0.199  0.929  0.993
      4       zoo  0.232  0.112  0.212
      5      zoom  0.842  0.620  0.344
      6  zucchini  0.945  0.265  0.745
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 2019-09-01
        • 2019-03-29
        • 1970-01-01
        • 2017-01-21
        • 1970-01-01
        相关资源
        最近更新 更多