Want MultiIndex for rows and columns with read_csv

廉价感情. 提交于 2021-01-28 12:57:01

问题


My .csv file looks like:

Area    When    Year    Month   Tickets
City    Day     2015    1       14
City    Night   2015    1       5
Rural   Day     2015    1       18
Rural   Night   2015    1       21
Suburbs Day     2015    1       15
Suburbs Night   2015    1       21
City    Day     2015    2       13

containing 75 rows. I want both a row multiindex and column multiindex that looks like:

Area         City        Rural         Suburbs
When         Day Night   Day Night     Day Night
Year Month
2015 1       5.0   3.0  22.0  11.0    13.0   2.0
     2      22.0   8.0   4.0  16.0     6.0  18.0
     3      26.0  25.0  22.0  23.0    22.0   2.0
2016 1      20.0  25.0  39.0  14.0     3.0  10.0
     2       4.0  14.0  16.0  26.0     1.0  24.0
     3      22.0  17.0   7.0  24.0    12.0  20.0 

I've read the .read_csv doc at https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

I can get the row multiindex with:

df2 = pd.read_csv('c:\\Data\Tickets.csv', index_col=[2, 3])

I've tried:

df2 = pd.read_csv('c:\\Data\Tickets.csv', index_col=[2, 3], header=[1, 3, 5])

thinking [1, 3, 5] fetches 'City', 'Rural', and 'Suburbs'. How do I get the desired column multiindex shown above?


回答1:


Seems like you need to pivot_table with multiple indexes and multiple columns.

Start with just reading you csv plainly

df = pd.read_csv('Tickets.csv')

Then

df.pivot_table(index=['Year', 'Month'], columns=['Area', 'When'], values=['Tickets'])

With the input data you provided, you'd get

Area             City           Rural            Suburbs
When             Day    Night   Day     Night    Day    Night
Year    Month                       
2015    1        14.0   5.0     18.0    21.0     15.0   21.0
        2        13.0   NaN     NaN     NaN      NaN    NaN


来源:https://stackoverflow.com/questions/52435825/want-multiindex-for-rows-and-columns-with-read-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!