简体   繁体   English

熊猫使用字符串包含来匹配2个数据框中的值

[英]Pandas Using String Contains to match values in 2 dataframes

Lets say I have 2 dataframes with names of cities but with different formats. 可以说我有2个数据框,其中包含城市名称,但格式不同。 So, I want to match them based on their states, and the first four characters of each city name. 因此,我想根据他们的州和每个城市名称的前四个字符来匹配它们。 A small example is as follows: 一个小例子如下:

import pandas as pd
df1 = pd.DataFrame({'city': ['NEW YORK', 'DALLAS', 'LOS ANGELES', 'SAN FRANCISCO'],
                   'state' : ['NY', 'TX', 'CA', 'CA'],
                   'value' : [1,2,3,4]})
df2 = pd.DataFrame({'city': ['NEW YORK CITY', 'DALLAS/ABC', 'LOS ANG', 'ABC'],
                    'state': ['NY', 'TX', 'CA', 'CA'],
                   'temp': [20,21,21,23]})
df1
        city    state   value
    0   NEW YORK    NY  1
    1   DALLAS  TX  2
    2   LOS ANGELES CA  3
    3   SAN FRANCISCO   CA  4

df2 
    city    state   temp
0   NEW YORK CITY   NY  20
1   DALLAS/ABC  TX  21
2   LOS ANG CA  21
3   ABC CA  23

What I want is a dataframe as follows: 我想要的是一个数据框,如下所示:

city    state   temp    values
0   NEW YORK    NY  20  1
1   DALLAS  TX  21  2
2   LOS ANG CA  21  3

Now, it follows that I cannot use the isin() since that will since that will result in the city names not matching. 现在,我不能使用isin() ,因为那样会导致城市名称不匹配。 So far, I am thinking of using str.contains but cannot think of an efficient way to do this. 到目前为止,我正在考虑使用str.contains但无法想到一种有效的方法来执行此操作。

Help is greatly appreciated. 非常感谢您的帮助。

Create a temporary city4 column with 4 character to use merge 创建一个具有4个字符的临时city4列以使用merge

In [5247]: pd.merge(df1.assign(city4=df1.city.str[:4]),
                    df2.assign(city4=df2.city.str[:4]), 
                    on=['city4', 'state']).drop('city4', 1)
Out[5247]:
        city_x state  value         city_y  temp
0     NEW YORK    NY      1  NEW YORK CITY    20
1       DALLAS    TX      2     DALLAS/ABC    21
2  LOS ANGELES    CA      3        LOS ANG    21

More specifically. 进一步来说。

In [5251]: (pd.merge(df1.assign(city4=df1.city.str[:4]),
      ...:           df2.assign(city4=df2.city.str[:4]),
      ...:           on=['city4', 'state'])
              .drop(['city4', 'city_y'], 1)
              .rename(columns={'city_x': 'city'}))
Out[5251]:
          city state  value  temp
0     NEW YORK    NY      1    20
1       DALLAS    TX      2    21
2  LOS ANGELES    CA      3    21

Details 细节

In [5255]: df1.assign(city4=df1.city.str[:4])
Out[5255]:
            city state  value city4
0       NEW YORK    NY      1  NEW
1         DALLAS    TX      2  DALL
2    LOS ANGELES    CA      3  LOS
3  SAN FRANCISCO    CA      4  SAN

In [5256]: df2.assign(city4=df2.city.str[:4])
Out[5256]:
            city state  temp city4
0  NEW YORK CITY    NY    20  NEW
1     DALLAS/ABC    TX    21  DALL
2        LOS ANG    CA    21  LOS
3            ABC    CA    23   ABC

one way using map by creating keys using state and 4 letters of city ie 通过使用州和城市的4个字母创建密钥创建地图的一种方法,即

one = df1.state+df1.city.str[:4]
two = df2.state+df2.city.str[:4]
df1['temp']=(one).map(df2.set_index(two)['temp'].to_dict())
df1 = df1.dropna()
city state  value  temp
0     NEW YORK    NY      1  20.0
1       DALLAS    TX      2  21.0
2  LOS ANGELES    CA      3  21.0

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

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