需要带有 read

编程入门 行业动态 更新时间:2024-10-15 04:28:05
本文介绍了需要带有 read_csv 的行和列的 MultiIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的 .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 Night 2015 1 21 Suburbs Day 2015 1 15 Suburbs Night 2015 1 21 City Day 2015 2 13

包含 75 行.我想要一个行多索引和列多索引,看起来像:

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

我在 https 上阅读了 .read_csv 文档://pandas.pydata/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])

我试过了:

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

thinking [1, 3, 5] 获取城市"、农村"和郊区".如何获得上面显示的所需列多索引?

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

推荐答案

似乎您需要pivot_table 具有多个索引和多个列.

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

从简单地读取您的 csv 开始

Start with just reading you csv plainly

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

然后

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

更多推荐

需要带有 read

本文发布于:2023-11-12 10:36:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!