pandas 阅读csv方向

编程入门 行业动态 更新时间:2024-10-18 10:21:22
本文介绍了 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方向

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

发布评论

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

>www.elefans.com

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