0

I have two pandas dataframes: df and temp_df. The df has a header, but temp_df does not have it.

df =

   IdTravel   TravelDate               IdProfesional        InformAsign
0  8178429    2017-10-25 11:25:16.550  NaN                  NaN
1  8180074    2017-10-25 13:49:09.640  NaN                  NaN
2  8180287    2017-10-25 14:28:04.000  123                  ABC
3  8182810    2017-10-26 09:55:14.930  NaN                  NaN
4  8182849    2017-10-26 09:59:11.187  NaN                  NaN

temp_df =

   0          1                        2                    3
0  4189915    2017-10-25 13:49:09.640  NaN                  NaN
1  4100334    2017-10-25 14:28:04.000  111                  ABC
2  4102833    2017-10-26 09:55:14.930  NaN                  NaN
3  4102845    2017-10-26 09:59:11.187  NaN                  NaN

I want to append temp_df to df. Expected result:

   IdTravel   TravelDate               IdProfesional        InformAsign
0  8178429    2017-10-25 11:25:16.550  NaN                  NaN
1  8180074    2017-10-25 13:49:09.640  NaN                  NaN
2  8180287    2017-10-25 14:28:04.000  123                  ABC
3  8182810    2017-10-26 09:55:14.930  NaN                  NaN
4  8182849    2017-10-26 09:59:11.187  NaN                  NaN
5  4189915    2017-10-25 13:49:09.640  NaN                  NaN
6  4100334    2017-10-25 14:28:04.000  111                  ABC
7  4102833    2017-10-26 09:55:14.930  NaN                  NaN
8  4102845    2017-10-26 09:59:11.187  NaN                  NaN

I tried:

result = df.append(temp_df)

I was expecting to get a new dataframe with 9 rows. But instead I got a dataframe result with 9 rows and 8 columns.

Also, I tried this, but got the same wrong result:

result = pd.concat([df,temp_df],axis=1,ignore_index=True) # and axis=0

1 Answer 1

1

You can rename columns in temp_df to match the original df:

temp_df.columns = ['IdTravel', 'TravelDate', 'IdProfesional', 'InformAsign']
result = df.append(temp_df).reset_index(drop=True)
print(result)

Prints:

   IdTravel               TravelDate  IdProfesional InformAsign
0   8178429  2017-10-25 11:25:16.550            NaN         NaN
1   8180074  2017-10-25 13:49:09.640            NaN         NaN
2   8180287  2017-10-25 14:28:04.000          123.0         ABC
3   8182810  2017-10-26 09:55:14.930            NaN         NaN
4   8182849  2017-10-26 09:59:11.187            NaN         NaN
5   4189915  2017-10-25 13:49:09.640            NaN         NaN
6   4100334  2017-10-25 14:28:04.000          111.0         ABC
7   4102833  2017-10-26 09:55:14.930            NaN         NaN
8   4102845  2017-10-26 09:59:11.187            NaN         NaN
Sign up to request clarification or add additional context in comments.

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.