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

Viewed 35

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)
3 Answers

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)

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 Multindex objects in pandas. You basically create the labels (called levels) and then use indexes to point to those labels (called codes) to build the object.

Here how to do it:

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) 

df1.columns = pd.MultiIndex(levels=[['df1', 'df2', 'df3'], ['Name', 'Age']], codes=[[0, 0], [0, 1]])
df2.columns = pd.MultiIndex(levels=[['df1', 'df2', 'df3'], ['Name', 'Age']], codes=[[1, 1], [0, 1]])
df3.columns = pd.MultiIndex(levels=[['df1', 'df2', 'df3'], ['Name', 'Age']], codes=[[2, 2], [0, 1]])

And after the concatenation, you will have:

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

enter image description here

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