python - Pandas:以 'column' 标题作为行元素读取时间序列数据的 CSV

标签 python csv pandas time-series

是否可以读取这种格式的 CSV 文件:

2013-01-01,A,1
2013-01-02,A,2
2013-01-03,A,3
2013-01-04,A,4
2013-01-05,A,5
2013-01-01,B,1
2013-01-02,B,2
2013-01-03,B,3
2013-01-04,B,4
2013-01-05,B,5

进入一个像这样结束的DataFrame:

             A   B
2013-01-01   1   1
2013-01-02   2   2
2013-01-03   3   3
2013-01-04   4   4
2013-01-05   5   5

我在 I/O 文档中看不到任何内容 ( http://pandas.pydata.org/pandas-docs/dev/io.html )

最佳答案

为什么不在读取 DataFrame 后 reshape (旋转)?

In [1]: df = pd.read_csv('foo.csv', sep=',', parse_dates=[0], header=None,
                         names=['Date', 'letter', 'value'])

In [2]: df
Out[2]: 
                 Date letter  value
0 2013-01-01 00:00:00      A      1
1 2013-01-02 00:00:00      A      2
2 2013-01-03 00:00:00      A      3
3 2013-01-04 00:00:00      A      4
4 2013-01-05 00:00:00      A      5
5 2013-01-01 00:00:00      B      1
6 2013-01-02 00:00:00      B      2
7 2013-01-03 00:00:00      B      3
8 2013-01-04 00:00:00      B      4
9 2013-01-05 00:00:00      B      5

In [3]: df.pivot(index='Date', columns='letter', values='value')
Out[3]:
letter      A  B
Date            
2013-01-01  1  1
2013-01-02  2  2
2013-01-03  3  3
2013-01-04  4  4
2013-01-05  5  5

关于python - Pandas:以 'column' 标题作为行元素读取时间序列数据的 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19183304/

相关文章:

python - 如何使用 python 请求通过另一个代理重试连接?

linux - 在 Linux 中,是否有将 CSV 文件转换为 SQLite 文件的命令?

html - 在 HTML 中使用 pandas 数据框

python - 在python中使用查询时出现问题

python - Django 没有在 Windows 机器上正确安装

python - 检查 gdb pretty-print 中的内存

Python从字符串列表中搜索确切的单词?

java - 是否可以使用 Apache Camel 实现服务器推送?

python - 在 python 中按数字对字符串列表进行排序

python - 对 pandas 多重索引进行排序