0

I have a csv file which is missing the first two column names, therefore looking like that:

,,MedKD,AvgKD,Q,AKD,PKD(.2),GPKD(.2),FS
Genomics,First Query,0.007704,0.008301,0.005379,0.002975,0.000551,0.000547,0.000100
Genomics,PayOff,44,51,62,17,82,3,-
Genomics,Convergence,-,-,*,*,4062,*,-
Genomics,Robustness,-,-,8E-07,3E-07,5E-08,1E-08,-
Genomics,Time,0.968427,1.007088,1.445911,0.313453,1.088019,1.142605,2.277355
Power,First Query,0.000084,0.000049,0.000034,0.000035,0.000016,0.000016,0.000014
Power,PayOff,4,2,2,1,0,0,-
Power,Convergence,-,-,7,12,16,1993,-
Power,Robustness,-,-,9E-11,7E-11,2E-11,2E-11,-
Power,Time,0.017216,0.023430,0.019345,0.022128,0.017878,0.019799,0.044971

When reading into a pandas dataframe, it therefore looks like that: enter image description here

I have tried all sorts of ways to give the first two a name, but every time, I just overwrote the other ones, but did not touch the first ones. Example:

m.columns = ['Dataset', 'Measure'] + m.columns[2:].tolist()

This only results in moving all the other ones to the right, starting with MedKD

How is it possible to insert those by using pandas?

Hemmelig
  • 803
  • 10
  • 22
  • Does this answer your question? [Python Pandas: How to set the name of multiindex?](https://stackoverflow.com/questions/49505080/python-pandas-how-to-set-the-name-of-multiindex) – itprorh66 Dec 08 '22 at 14:35
  • I have been there. I doesn't fully resolve the problem since it adds names to these, but not on the same "level", then I have two levels of indexes – Hemmelig Dec 08 '22 at 14:37
  • Yes, exactly that's because the df you show has two levels of indexing, if you don't want two levels you need to drop the index level you don't want see [pandas.MultiIndex.droplevel](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.MultiIndex.droplevel.html) for appropriate methodology. – itprorh66 Dec 08 '22 at 14:39
  • Yeah but dropping it just makes it disappear. Also, I tried "df.columns.to_flat_index()", which literally made nothing – Hemmelig Dec 08 '22 at 14:45

0 Answers0