我基本上试图做以下事情:
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)
问题在于您使用的代码将county_name转换为lower。 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
如果要通过'county_name'合并那些,请使用list将map函数值转换为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')