Home > database >  How to add a row to mention the names of the dataframes after we concatenate them?
How to add a row to mention the names of the dataframes after we concatenate them?

Time:02-25

I have 3 dataframes with the same format.

enter image description here

Then I combine them horizontally and get

enter image description here

I would like to add a row to denote the name of each dataframe, i.e.,

enter image description here

I get above form by copying the data to MS Excel and manually adding the row. Is there anyway to directly do so for displaying in Python?

import pandas as pd

data = {'Name': ['Tom', 'Joseph'], 'Age': [20, 21]}  
df1 = pd.DataFrame(data) 

data = {'Name': ['John', 'Kim'], 'Age': [15, 17]}  
df2 = pd.DataFrame(data)

data = {'Name': ['Paul', 'Dood'], 'Age': [10, 5]}  
df3 = pd.DataFrame(data) 

pd.concat([df1, df2, df3], axis = 1)

CodePudding user response:

The row is actually a first-level column. You can have it by adding this level to each dataframe before concatenating:

for df_name, df in zip(("df1", "df2", "df3"), (df1, df2, df3)):
    df.columns = pd.MultiIndex.from_tuples(((df_name, col) for col in df))

pd.concat([df1, df2, df3], axis = 1)

CodePudding user response:

Very nich case, but you can use Multindex objects in order to be able to build want you want.

Consider that what you need is a "two level headers" to display the information as you want. Multindex at a columns level can accomplish that. To understand more the code, read about enter image description here

CodePudding user response:

Use key parameter in concat:

df = pd.concat([df1, df2, df3], axis = 1, keys=('df1','df2','df3'))
print (df)
      df1       df2       df3    
     Name Age  Name Age  Name Age
0     Tom  20  John  15  Paul  10
1  Joseph  21   Kim  17  Dood   5
  • Related