0

At the moment i try to read a *.txt with "read_csv". Works fine so far.

In[1]: df = pd.read_csv('Data.txt', skiprows=range(0,4), sep='\t', header = 0, skipinitialspace = True)

If i set the header = 0 i get the Elementlabels, but they repeat for each value of CTF1, CTF2, CTF3... and so on. So there are multiple Elements in the Header with the same value:

20052065, 20052065 .1, 20052065 .2 ... --> 20052065 .11

In[2]: print(df)

Out[2]:

      Unnamed: 0  ELEMENT  20052065  20052066 20052082  20052087 20052089  \
   0     TIME[s]  TEMP[C]      CTF1      CTF1     CTF1      CTF1     CTF1   
   1       0.000   24.000    -4.234    -6.728  -14.386    -4.356   -6.926   
   2      60.000   36.137   -29.308   -24.795  -26.937   -30.134  -24.735   
   3     120.000   49.013   -48.825   -36.383  -29.986   -49.897  -35.748   

      20052090 20052116 20052119     ...     20052116.10 20052119.10 20052065.11  \
   0      CTF1     CTF1     CTF1     ...             CU3         CU3        CU_M   
   1   -10.205   -9.934  -14.012     ...           0.001       0.001       0.003   
   2   -23.474  -23.982  -27.175     ...          -0.016      -0.015       0.023   
   3   -28.007  -28.904  -29.788     ...          -0.035      -0.032       0.036   

So i would like to create a MultiIndex with CTF1, CTF2, CTF3,... as the "upper" Index and down there the Elementlabels. In the end i would like to select a Value by its 1. Level and its 2. Level Index. Got no Idea how to get this work. :-/

The *.txt looks like:

enter image description here

Nils
  • 409
  • 6
  • 16
  • 1
    Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your post correspondingly. – MaxU - stand with Ukraine Mar 25 '18 at 10:27
  • Possible duplicate of [Load CSV to Pandas MultiIndex DataFrame](https://stackoverflow.com/questions/19103624/load-csv-to-pandas-multiindex-dataframe) – RK1 Mar 25 '18 at 10:33
  • RK1... index_col doesn´t work so far, cause there is no multiIndex in the column. It´s in the header (row). MaxU thanks for the hint. I will edit it later. – Nils Mar 25 '18 at 10:45
  • 1
    It seems neec change `header = 0,` to `header = [0,1],` – jezrael Mar 25 '18 at 11:10
  • In my opinion without real data, your file is really problematic test `read_csv`. Can you share one file? – jezrael Mar 25 '18 at 11:17
  • jezrael with `header = [0,1]` it works pretty well. Afterwards i can select the entries this way: `print(df['20052082']['CTF1'])`. I´ve tried to select different values for the Output like: `print(df['20052082']['CTF1', 'CTF2'])`. Do you even know how this works? Thanks for Help! – Nils Mar 25 '18 at 12:00

0 Answers0