0

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
Pyd
  • 6,017
  • 18
  • 52
  • 109
  • Check the link , already have detail and answer there. https://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html – BENY Aug 08 '17 at 13:57

2 Answers2

1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • can you check this one https://stackoverflow.com/questions/45583113/comparing-two-dataframe-columns-to-check-if-they-have-same-value-in-python – Pyd Aug 09 '17 at 06:31
1

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

Etezadi
  • 111
  • 1
  • 5