Skip to content Skip to sidebar Skip to footer

Want Multiindex For Rows And Columns With Read_csv

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 Nig

Solution 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
WhenDay    Night   Day     Night    Day    Night
YearMonth2015114.05.018.021.015.021.0213.0   NaN     NaN     NaN      NaN    NaN

Post a Comment for "Want Multiindex For Rows And Columns With Read_csv"