0

Input
Df1:

 A B C a 2 4 4 b 1 6 3 c 4 2 8 

Df2:

P Q R S T 5 3 8 3 0 4 8 6 8 3 

Desired Output

 A B C a 2 4 4 b 1 6 3 c 4 2 8 P Q R S T 5 3 8 3 0 4 8 6 8 3 

I want to connect Df1 and Df2 one below each other as shown above.
How can I do that?

6
  • would the [P Q R S T] be a row in the final dataframe? Commented Sep 2, 2020 at 14:23
  • 2
    Off topic : What is the use case for this? Just curious why do you want this. Are you sure this isn't a XY Problem Commented Sep 2, 2020 at 14:24
  • When concatenating/appending two dataframes they need to have similar column names. So you would have rename Df2 columns then concat them. see stackoverflow.com/questions/48792037/… Commented Sep 2, 2020 at 14:35
  • @DavidGibson No I cant not give extra columns name to any of the DataFrame. Commented Sep 2, 2020 at 19:21
  • @anky I am saving these two DataFrame in .csv format on the same sheet and same manner Commented Sep 2, 2020 at 19:23

1 Answer 1

1

Not sure why you need it, but the code below will make the trick:

 import pandas as pd # initialize dataframes df1 = pd.DataFrame([[2, 4, 4], [1, 6, 3], [4, 2, 8]], columns=['A', 'B', 'C'], index=['a', 'b', 'c']) df2 = pd.DataFrame([[3, 8, 3, 0], [8, 6, 8, 3]], columns=['Q', 'R', 'S', 'T'], index=[5, 4]) df2.index.name = 'P' # prepare for concat df2.loc[df2.index.name] = df2.columns df2.index = df2.index.astype(str) df2 = df2.sort_index(ascending=False) df2.columns = ['A', 'B', 'C', ''] df2.index.name = '' df1[''] = ['','',''] # concat df = pd.concat([df1, df2]) 

Output:

In [2]: df1 Out[2]: A B C a 2 4 4 b 1 6 3 c 4 2 8 In [3]: df2 Out[3]: A B C P Q R S T 5 3 8 3 0 4 8 6 8 3 In [4]: df Out[4]: A B C a 2 4 4 b 1 6 3 c 4 2 8 P Q R S T 5 3 8 3 0 4 8 6 8 3 
Sign up to request clarification or add additional context in comments.

2 Comments

My DataFrame shapes are Df1 (9 * 3) & Df2 ( 8760 * 7). I have tried but not working
I would like to help, however the code in the answer does exactly what you asked in the question. Could you maybe be more precise and explain what you actually need?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.