简体   繁体   English

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

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

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: 进入一个像这样结束的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 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

Why not reshape (pivot) after you've read in the DataFrame? 在读取DataFrame 后,为什么不重塑(pivot)?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果列表示月份并且索引是年份,如何按行读取 Pandas 的 dataframe 和 plot 中的值作为时间序列? - How to read the data in Pandas's dataframe row wise and plot the values as a timeseries if column represents months and index is years? 使用Dask DataFrame和Pandas有效地读取时间序列CSV数据目录 - Read Directory of Timeseries CSV data efficiently with Dask DataFrame and Pandas 熊猫-读取数据(两行标头,索引列) - Pandas - read data (two-row header, index column) 熊猫:CSV标头和数据行大小不匹配 - Pandas: CSV header and data row size mismatch 防止 pandas read_csv 将第一行视为列名的标题 - Prevent pandas read_csv treating first row as header of column names 将时间序列数据中的行转换为列 - Converting row in timeseries data to column 熊猫read_csv,读取缺少标头元素的csv文件 - Pandas read_csv, reading a csv file with a missing header element 跳过第一行并在熊猫中使用第二行作为标头来获取符号的原始刻度数据时无法读取 csv - Not able to read csv while skipping first row and using second as header in pandas for raw tick data of symbols 熊猫中的read_csv-如何使用特定行作为标题 - read_csv in pandas - how to use a specific row as header "ValueError:索引 DATE 无效,标题行上有 pandas.read_csv" - ValueError: Index DATE invalid with pandas.read_csv on header row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM