Pyd
Pyd

Reputation: 6159

concatinating two DataFrames without NaN in python

I have two dataframes,

df_temp,
       Age  Name    city
   0    1   Pechi   checnnai
   1    2   Sri     pune

df_po

        po
   0    er
   1    ty

I tried pd.concat([df_temp,df_po])

 df_temp=pd.concat([df_temp,df_po],ignore_index=True)

I am getting

        Age Name    city        po
   0    1.0 Pechi   checnnai    NaN
   1    2.0 Sri     pune        NaN
   2    NaN NaN     NaN         er
   3    NaN NaN     NaN         ty

but my desired output is,

        Age Name    city        po
   0    1.0 Pechi   checnnai    er
   1    2.0 Sri     pune        ty

Upvotes: 0

Views: 987

Answers (2)

Etezadi
Etezadi

Reputation: 141

You should use: ignore_index=True for sure

I also recommend that reset both data frames before concat command

result = pd.concat([df1, df2], axis=1, ignore_index=True)

Good Luck

Upvotes: 1

jezrael
jezrael

Reputation: 862681

Need axis=1 for columns concatenate , because default is axis=0 (index concatenate) in concat:

df_temp=pd.concat([df_temp,df_po],axis=1)
print (df_temp)
   Age   Name      city  po
0    1  Pechi  checnnai  er
1    2    Sri      pune  ty

Alternative solution with DataFrame.join:

df_temp=df_temp.join(df_po)
print (df_temp)
   Age   Name      city  po
0    1  Pechi  checnnai  er
1    2    Sri      pune  ty

Upvotes: 1

Related Questions