In pandas I would like to have a dataframe whose some columns have a multi index, some don't.
Visually I would like something like this:
| c | |
|--------| d |
| a | b | |
================|
| 1 | 4 | 0 |
| 2 | 5 | 1 |
| 3 | 6 | 2 |
In pandas I can do something like this:
df = pd.DataFrame({'a':[1,2,3],'b':[4,5,6], 'd':[0,1,2]})
columns=[('c','a'),('c','b'), 'd']
df.columns=pd.MultiIndex.from_tuples(columns)
and the output would be:
c d
a b NaN
0 1 4 0
1 2 5 1
2 3 6 2
However, when accessing the d column by df['d'], I get as output a Pandas Dataframe, not Pandas series. The problem is clearly that pandas applied multicolumn indexing to every column. So my question is: is there a way to apply column multindexing only to certain columns and leave the others as they are?
In other words, I would like that the result of df['d'] would be a Series as in a normal dataframe, the result of df['c'] a pd.DataFrame as in column multindex and the result of df['c']['a'] a Pandas Series. Is this possible?
You can use the empty string
""as a placeholder :Output :