【问题标题】:Replacing values in one pandas dataframe with values from another dataframe用另一个数据框中的值替换一个熊猫数据框中的值
【发布时间】:2019-06-30 03:52:46
【问题描述】:

我必须将一个数据帧中的值替换为另一个数据帧中的值。

下面的示例有效,但我有额外的步骤来用“新”列中的值替换“第一”列中的值,而不是删除“新”列。

In [1]: import pandas as pd                                                                                                  

In [2]: df = pd.DataFrame([['A', 'X'], 
   ...:                    ['B', 'X'], 
   ...:                    ['C', 'X'], 
   ...:                    ['A', 'Y'], 
   ...:                    ['B', 'Y'], 
   ...:                    ['C', 'Y'], 
   ...:                    ], columns=['first', 'second'])                                                                   

In [3]: df                                                                                                                   
Out[3]: 
  first second
0     A      X
1     B      X
2     C      X
3     A      Y
4     B      Y
5     C      Y

In [4]: df_tt = pd.DataFrame([['A', 'E'], 
   ...:                       ['B', 'F'], 
   ...:                      ], columns=['orig', 'new'])                                                                     

In [5]: df_tt                                                                                                                
Out[5]: 
  orig new
0    A   E
1    B   F

In [6]: df = df.merge(df_tt, left_on='first', right_on='orig')                                                               

In [7]: df                                                                                                                   
Out[7]: 
  first second orig new
0     A      X    A   E
1     A      Y    A   E
2     B      X    B   F
3     B      Y    B   F

In [8]: df['first'] = df['new']                                                                                              

In [9]: df                                                                                                                   
Out[9]: 
  first second orig new
0     E      X    A   E
1     E      Y    A   E
2     F      X    B   F
3     F      Y    B   F

In [10]: df.drop(columns=['orig', 'new'])                                                                                    
Out[10]: 
  first second
0     E      X
1     E      Y
2     F      X
3     F      Y

我想替换值而不需要额外的步骤。

【问题讨论】:

    标签: python pandas merge


    【解决方案1】:

    使用isinboolean indexing 进行过滤,然后使用map

    df = (df[df['first'].isin(df_tt['orig'])]
             .assign(first=lambda x: x['first'].map(df_tt.set_index('orig')['new'])))
    print (df)
      first second
    0     E      X
    1     F      X
    3     E      Y
    4     F      Y
    

    替代方案:

    df = df[df['first'].isin(df_tt['orig'])]
    df['first'] = df['first'].map(df_tt.set_index('orig')['new'])
    

    【讨论】:

    • 和合并一样快吗?合并速度很快。
    • @user3225309 - 速度更快,最好用真实数据进行测试
    【解决方案2】:

    另一种解决方案是使用replace

    # Restrict to common entries
    df = df[df['first'].isin(df_tt['orig'])]
    
    # Use df_tt as a mapping to replace values in df
    
    df['first'] = df['first'].replace(df_tt.set_index('orig').to_dict()['new'])
    

    解决方案与@jezrael 非常相似,但我喜欢明确使用replace 的想法,因为这实际上是您正在做的事情:根据另一个数据帧替换一个数据帧中的值。

    【讨论】:

    • 你的解决方案和 jezrael 的解决方案一样好。多谢你们。实际上,您的解决方案也可以在没有“to_dict”的情况下使用: df['first'] = df['first'].replace(df_tt.set_index('orig')['new'])
    猜你喜欢
    • 2020-07-31
    • 2015-01-19
    • 2023-04-03
    • 2017-11-01
    • 1970-01-01
    相关资源
    最近更新 更多