Home > other >  Dataframes Not Merging properly. Getting an extra column
Dataframes Not Merging properly. Getting an extra column

Time:06-14

I want to join Dataframes with same column names into a single dataframe. When I press " df = pd.concat([df1, df2], ignore_index=True, sort=False)", I get an extra column. Please help enter image description here

This is how I am Getting the Concatenated Dataframe

CodePudding user response:

I think problem is State column has space in one DataFrame like State or State , so after concat was not join with State column.

Solution is:

#test columns names
print (df1.columns)
print (df2.columns)

#remove trailing spaces
df1.columns = df1.columns.str.strip()
df2.columns = df2.columns.str.strip()
df = pd.concat([df1, df2], ignore_index=True, sort=False)

CodePudding user response:

It looks like it's not recognizing the "state" as the same field. I'd probably start by taking a look at the setup of the state field in each table to see why it thinks they're different. If you can find a different, format them to be the same and then try again.

  • Related