How to join the lines of two Dataframes with Python?

Asked

Viewed 6,569 times

1

Hello! I need to join lines of two dataframes with python - pandas. For example, let’s say I have these two dataframes

X |  Y  |  Z
1 |  2  |  3
4 |  5  |  6 

and

A |  B  |  C
7 |  8  |  9
0 |  0  |  0

now I need to join the two dataframes, transforming into the following dataframe:

X  |  Y  |  Z
1  |  2  |  3
4  |  5  |  6
7  |  8  |  9 
0  |  0  |  0

That is, I need to delete the header of the dataframe 02 and add the other lines in the dataframe 01. Can anyone help me?

  • I managed to solve it myself. The command is as follows::

1 answer

3

There is the function .concat() of pandas that concatenates two dataframes, but to use it, the columns must have the same name. So we can rename them with the .rename(), another function of pandas.

import pandas as pd
relacao_colunas = {'A':'X', 'B':'Y', 'C':'Z'}
df_concat = pd.concat([df1, df2.rename(columns=relacao_colunas)])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.