使用pd.merge映射两个数据框

时间:2018-09-19 02:47:15

标签: python pandas dataframe merge mapping

我基本上正在尝试执行以下操作:

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)没有行。我正在尝试为我的每个县名分配一个陷阱。

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)

2 个答案:

答案 0 :(得分:1)

问题与您用来将county_name转换为小写的代码有关。 Map返回一个迭代器,您需要将其存储为数据类型。另外,在使用熊猫时,只需使用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

答案 1 :(得分:0)

如果要通过“ county_name”合并,请使用list将映射函数值转换为list。我们不能在合并数据框时使用类值。

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')