GK21
GK21

Reputation: 59

Grouping multiple columns under same header in dataframe

These are my 3 current dataframes with same columns -

TP FP     TP FP     TP FP
 1  3      8 11      4  7
22 14     10 13      0 19 

Here's how I want it in a single dataframe-

OCT-19    NOV-19    DEC-19
TP  FP    TP  FP    TP FP
 1   3     8  11     4  7
22  14    10  13     0 19 

Upvotes: 1

Views: 666

Answers (1)

Richard X
Richard X

Reputation: 1134

headers = pd.MultiIndex.from_product([["OCT-19"], ["TP", "FP"]])
df1 = pd.DataFrame([[1, 3], [22, 14]], columns=headers)

Just use MultiIndex.from_product() to specify the headers you want for your columns. Then you can pass these headers into the columns of your dataframe.

Upvotes: 1

Related Questions