I have two csv files. I was trying to merge them. The fist csv file 'Test_base1' looks like:
OBJECTID,STATEFP,COUNTYFP,TRACTCE,GEOID
1,12,105,10300,15000US121050103002
2,12,103,24804,15000US121030248041
3,12,105,10800,15000US121050108001
The second csv file 'Test_file23_1' looks like:
GEOID,B23003e1,B23003m1,B23003e2,B23003m2,B23003e3
15000US121050103002,69,81,13,21,13
15000US121030248041,657,248,62,79,0
15000US120010004001,410,143,261,126,47
while I was merging them with reference to field 'GEOID', I am getting an error: KeyError: 'GEOID'. But the 'GEOID' is present in the column name. Whats the solution? What is the problem with my code?
Update: It's not a coding error. Had a chat with @Zev. We figured it out that, it's not a coding problem. If I copy the data of files to a new black file, and create it, it worked. But I we do not know the cause behind it. Zev tried with the same file. No error on his side. But same error in my side. Files: 1. https://www.dropbox.com/s/rg706ck6bdda4sa/Test_Base1.csv?dl=0 2. https://www.dropbox.com/s/yf49f2p7btkc56r/Test_File23_1.csv?dl=0
import pandas as pd, numpy as np
from functools import reduce
df1=pd.read_csv("Test_Base1.csv")
df2=pd.read_csv("Test_File23_1.csv")
dfs = [ df1, df2]
df_merged = reduce(lambda left,right: pd.merge(left,right,on=['GEOID'],how='outer'), dfs)
df_merged.to_csv('test1.csv', index=False)