【问题标题】:Mapping two dataframes with pd.merge使用 pd.merge 映射两个数据帧
【发布时间】:2019-02-23 02:55:56
【问题描述】:

我基本上是在尝试做以下事情:

import pandas as pd

initial_df = {'county': ['REAGAN', 'UPTON', 'HARDEMAN', 'UPTON'], 'values': [508, 
364, 26, 870]}

intermediate_df = {'county': ['REAGAN', 'HARDEMAN', 'UPTON'], 'fips': [48383, 47069, 
48461]}

final_df = {'county': ['REAGAN', 'UPTON', 'HARDEMAN', 'UPTON'], 'fips': [48383, 
48461, 47069, 48461], 'values': [508, 364, 26, 870]}


df1=pd.DataFrame(initial_df)
df2=pd.DataFrame(intermediate_df)
df3=df1.merge(df2)

但是,当我尝试将相同的概念应用于我的实际数据时,我的最终数据框 (df3) 没有行。我试图为我的每个县名分配一个 fips。

df1 = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/master/Crude%20Oil%2C%20Gas%20Well%20Liquids%20or%20Associated%20Products%20(H-8)%20Loss%20Reports/all-geocodes-v2016.csv', encoding='latin-1')
df2 = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/master/Crude%20Oil%2C%20Gas%20Well%20Liquids%20or%20Associated%20Products%20(H-8)%20Loss%20Reports/h8s-2018.csv')

df2['county_name'] = map(str.lower, df2['county_name'])
df1['county_name'] = map(str.lower, df1['county_name'])

df1['fips_county'] = df1['fips_county'].apply(lambda x: str(int(x)).zfill(3))
df1['fips'] = df1.apply(lambda x:'%s%s' % (x['fips_state'],x['fips_county']),axis=1)

df3=df2.merge(df1)

【问题讨论】:

标签: python pandas dataframe merge mapping


【解决方案1】:

问题在于您用于将county_name 转换为更低的代码。 Map 返回一个迭代器,您需要将其存储在数据类型中。另外,当您使用 pandas 时,您可以简单地使用 pandas str 方法。

df2['county_name'] = df2['county_name'].str.lower()
df1['county_name'] = df1['county_name'].str.lower()

df1['fips_county'] = df1['fips_county'].astype(str).str.zfill(3)
df1['fips'] = df1[['fips_state','fips_county']].astype(str).apply(lambda x: ''.join(x), axis=1)

df1.merge(df1)

你得到

    fips_state  fips_county county_name fips
0   48          000         texas       48000
1   48          001         anderson    48001
2   48          003         andrews     48003
3   48          005         angelina    48005

【讨论】:

    【解决方案2】:

    如果你想通过'county_name'合并那些映射函数值到列表,使用列表。我们不能在合并数据框时使用类值。

    df1 = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/master/Crude%20Oil%2C%20Gas%20Well%20Liquids%20or%20Associated%20Products%20(H-8)%20Loss%20Reports/all-geocodes-v2016.csv', encoding='latin-1')
    df2 = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/master/Crude%20Oil%2C%20Gas%20Well%20Liquids%20or%20Associated%20Products%20(H-8)%20Loss%20Reports/h8s-2018.csv')
    
    df2['county_name'] = list(map(str.lower, df2['county_name']))
    df1['county_name'] = list(map(str.lower, df1['county_name']))
    
    df1['fips_county'] = df1['fips_county'].apply(lambda x: str(int(x)).zfill(3))
    df1['fips'] = df1.apply(lambda x:'%s%s' % (x['fips_state'],x['fips_county']),axis=1)
    
    df2.merge(df1,on=['county_name'],how='outer')
    

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多