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
1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.