Pandas:以“列”标题作为行元素读取时间序列数据的 CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19183304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:13:10  来源:igfitidea点击:

Pandas: Read a CSV of timeseries data with 'column' header as row element

pythoncsvpandastime-series

提问by Steve Pike

Is it possible to read a CSV file in this format:

是否可以读取这种格式的 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

into a DataFrame that ends up like this:

进入一个像这样结束的数据帧:

             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 couldn't see anything in the I/O docs (http://pandas.pydata.org/pandas-docs/dev/io.html)

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

回答by Andy Hayden

Why not reshape (pivot) afteryou've read in the DataFrame?

读完 DataFrame后,为什么不重塑(枢轴)?

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