【问题标题】:How do I merge two data frames in pandas on a common column which have similar values (but not the same)?如何在具有相似值(但不相同)的公共列上合并 pandas 中的两个数据框?
【发布时间】:2021-02-22 22:38:59
【问题描述】:

我正在尝试将 pandas 中的两个数据框合并到一个显示地理区域名称的公共列上。该列具有相似的值但不相同。例如,一个 DataFrame 中的值为London,而另一个为London / Greater London - 它们被视为不同的值,但在合并时应视为相同的值。

In[1]: 
import pandas as pd
df1 = pd.DataFrame([['London', 2], ['Bristol', 3], ['Liverpool', 6]], columns=['Area', 'B'])
df2 = pd.DataFrame([['London / Greater London', 7], ['Bristol_', 9], ['Liverpool / Liverpool', 1]], columns=['Area', 'B'])
df_merged = pd.merge(df1, df2, on="Area", indicator=True, how='outer')
df_merged

Out[1]: 
                      Area  B_x  B_y      _merge
0                   London  2.0  NaN   left_only
1                  Bristol  3.0  NaN   left_only
2                Liverpool  6.0  NaN   left_only
3  London / Greater London  NaN  7.0  right_only
4                 Bristol_  NaN  9.0  right_only
5    Liverpool / Liverpool  NaN  1.0  right_only

理想的输出如下所示:

Out[1]: 
                      Area  B_x  B_y      _merge
0                   London  2.0  7.0   both
1                  Bristol  3.0  9.0   both
2                Liverpool  6.0  1.0   both

有没有办法根据值的某种程度的相似性来合并这两个数据框,以便 LondonLondon / Greater London 值被视为相同的值?谢谢!

【问题讨论】:

    标签: python pandas dataframe merge


    【解决方案1】:

    您可以先使用np.where() 创建两个包含重叠AreaCity 的索引的arrays。我使用list comprehension 检查每个City 是否存在in Areas 的列表并保存索引。

    注意:这仅适用于Areastring 包含City string。 (即 London 仅与 London / Greater London 匹配,如果此 area 包含单词 London

    代码:

    # Alter the column names B (present in both dfs to B_x and B_y )
    df1 = pd.DataFrame([['London', 2], ['Bristol', 3], ['Liverpool', 6]], columns=['Area', 'B_x'])
    df2 = pd.DataFrame([['London / Greater London', 7], ['Bristol_', 9], ['Liverpool / Liverpool', 1]], columns=['Area', 'B_y'])
    
    # Create indices of matching string patterns
    i, j = np.where([[city in area for area in df2['Area'].values] for city in df1['Area'].values])
    
    # Create new dataframe with found indices
    pd.DataFrame(np.column_stack([df1.iloc[i], df2.iloc[j]]), columns=df1.columns.append(df2.columns))
    

    结果

        Area        B_x     Area                    B_y
    0   London      2   London / Greater London     7
    1   Bristol     3   Bristol_                    9
    2   Liverpool   6   Liverpool / Liverpool       1
    

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2021-12-17
      • 2020-05-19
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多