pandas 阅读csv方向 [英] pandas reading csv orientation

查看:202
本文介绍了 pandas 阅读csv方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hei我想读的熊猫csv文件你可以从这里下载(euribor rate我想你可以想象我想要有这个文件的原因)。该文件是一个CSV文件,但它是以某种方式奇怪的取向。如果您在Excel文件中导入的格式为

Hei I'm trying to read in pandas the csv file you can download from here (euribor rates I think you can imagine the reason I would like to have this file!). The file is a CSV file but it is somehow strangely oriented. If you import it in Excel file has the format

   02/01/2012,03/01/2012,04/01/2012,,,, 
1w 0.652,0.626,0.606,,,,
2w,0.738,0.716,0.700,,,,

与第一列上升到12米(但我给你的链接,你可以下载一个示例)。我想读大熊猫,但我不能以正确的方式阅读它。 Pandas有一个内置的功能读取csv文件,但不知何故,它期望是行取向而不是列取向。我想做的是获取标记为3m的行的信息,并具有值和日期,以绘制此指数的时间变化。但我不能处理这个问题。我知道我可以用

act with first column going up to 12m (but I have give you the link where you can download a sample). I would like to read it in pandas but I'm not able to read it in the correct way. Pandas has a built-in function for reading csv files but somehow it expect to be row oriented rather than column oriented. What I would like to do is to obtain the information on the row labeled 3m and having the values and the date in order to plot the time variation of this index. But I can't handle this problem. I know I can read the data with

import pandas 
data = pandas.io.read_csv("file.csv",parse_dates=True) 

但它会工作,如果csv文件将以某种方式转置。 H

but it would work if the csv file would be somehow transpose. H

推荐答案

pandas数据框架有一个 .transpose()但它不喜欢此文件中的所有空行。下面是如何清理它:

A pandas dataframe has a .transpose() method, but it doesn't like all the empty rows in this file. Here's how to get it cleaned up:

df = pandas.read_csv("hist_EURIBOR_2012.csv")  # Read the file
df = df[:15]    # Chop off the empty rows beyond 12m
df2 = df.transpose()
df2 = df2[:88]  # Chop off what were empty columns (I guess you should increase 88 as more data is added.

当然,你可以链接在一起:

Of course, you can chain these together:

df2 = pandas.read_csv("hist_EURIBOR_2012.csv")[:15].transpose()[:88]

然后 df2 ['3m'] 但是日期仍然存储为字符串。我不太确定如何将它转换为 DateIndex

Then df2['3m'] is the data you want, but the dates are still stored as strings. I'm not quite sure how to convert it to a DateIndex.

这篇关于 pandas 阅读csv方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆