简体   繁体   中英

how to assign hierarchical column names in pandas dataframe

My CSV file has on the first 2 rows the names that I would like to use as column names and the first two columns as row names. So, the file looks like this:

tp,desc,L,L,D,D
,,1,2,3,4
3001, foo, 23.1, 35.3, 52.0, 11.9
3010, bar, 31.l, 25.9, 13.9, 134.8

I was able to set the first two columns as an index, but am stuck on getting the first two rows be accepted as column names

This is my input statement so far:

df = pd.read_csv("file.csv", index_col=[tp,desc])

Thanks.

Try to specify the " index " column using index_col , and decode the data you have to be able to read it.

from io import StringIO
import pandas as pd

data="tp,desc,L,L,D,D\n,,1,2,3,4\n3001, foo, 23.1, 35.3, 52.0, 11.9\n3010, bar, 31.l, 25.9, 13.9, 134.8"
df= pd.read_csv(StringIO(data.decode('UTF-8')),sep=',', index_col=[0,1])
print df

Output:

               L   L.1     D    D.1
tp   desc                          
NaN  NaN       1   2.0   3.0    4.0
3001  foo   23.1  35.3  52.0   11.9
3010  bar   31.l  25.9  13.9  134.8

Try to read the file and convert it this way. there are different solutions here . But usually this could solve the problem.

with open('example.csv', 'rb') as f:
    csv = f.read().decode("utf-8")

may be you could try with:

import pandas as pd

df = pd.read_csv('file.csv', header=None)

# getting names for columns and index:
cnames = zip(df.iloc[0,2:], df.iloc[1,2:])
inames = list(df.iloc[0,:2])    

#drop the rows with column names (for columns and index)
df.drop([0,1],axis=0,inplace=True)
#set the indexes
df.set_index([0,1],inplace=True)
# set the names for columns and indexes
df.columns = pd.MultiIndex.from_tuples(cnames)
df.index.names = inames

The result is:

               L             D        
               1      2      3       4
tp   desc                             
3001  foo   23.1   35.3   52.0    11.9
3010  bar   31.l   25.9   13.9   134.8

I used the following file content:

tp,desc,L,L,D,D
,,1,2,3,4
3001, foo, 23.1, 35.3, 52.0, 11.9
3010, bar, 31.l, 25.9, 13.9, 134.8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM