Home > other >  Python: How to add main column header for multiple column headings?
Python: How to add main column header for multiple column headings?

Time:07-06

Currently I have this data set:

data={'a1':[1,2,3,4,5],'a2':[4,5,6,7,8],'b1':[2,5,3,7,9],'b2':[7,5,8,9,3],'c1':[2,4,5,7,5]}
df=pd.DataFrame(data)
df

How can i script python if I would like to add main column headers to the existing column headings. For instance, 'a1' & 'a2 will be categorized under 'A', 'be' & 'b2' will be categorized under 'B' and 'c1' remains alone. For example:

enter image description here

Thank you in advance :)

CodePudding user response:

data={'a1':[1,2,3,4,5],'a2':[4,5,6,7,8],'b1':[2,5,3,7,9],'b2':[7,5,8,9,3],'c1':[2,4,5,7,5]}
df=pd.DataFrame(data)
df.columns=[['a','a','b','b','c'],['a1','a2','b1','b2','c1']]

CodePudding user response:

You can assign values to df.columns to get the desired output. You also get a single C for the c1 column:

df.columns = [[i[0].upper() for i in data.keys()], list(data.keys())]

Output:

   A     B     C
  a1 a2 b1 b2 c1
0  1  4  2  7  2
1  2  5  5  5  4
2  3  6  3  8  5
3  4  7  7  9  7
4  5  8  9  3  5

CodePudding user response:

Use pd.MultiIndex for adding another level of header

columns=[('a','a1'),('a','a2'),('b','b1'),('b','b2'),('c','c')]
df.columns=pd.MultiIndex.from_tuples(columns)
  • Related