读取csv文件并在Python中返回data.frame [英] read csv file and return data.frame in Python
本文介绍了读取csv文件并在Python中返回data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个CSV文件,value.txt
包含以下内容:
文件的前几行是:
I have a CSV file, "value.txt"
with the following content:
the first few rows of the file are :
Date,"price","factor_1","factor_2"
2012-06-11,1600.20,1.255,1.548
2012-06-12,1610.02,1.258,1.554
2012-06-13,1618.07,1.249,1.552
2012-06-14,1624.40,1.253,1.556
2012-06-15,1626.15,1.258,1.552
2012-06-16,1626.15,1.263,1.558
2012-06-17,1626.15,1.264,1.572
在R中,我们可以使用
price <- read.csv("value.txt")
,并将返回一个data.frame我可以用于统计操作:
and that will return a data.frame which I can use for statistical operations:
> price <- read.csv("value.txt")
> price
Date price factor_1 factor_2
1 2012-06-11 1600.20 1.255 1.548
2 2012-06-12 1610.02 1.258 1.554
3 2012-06-13 1618.07 1.249 1.552
4 2012-06-14 1624.40 1.253 1.556
5 2012-06-15 1626.15 1.258 1.552
6 2012-06-16 1626.15 1.263 1.558
7 2012-06-17 1626.15 1.264 1.572
有没有Pythonic的方法来获得相同的功能?
Is there a Pythonic way to get the same functionality?
推荐答案
pandas 救援:
import pandas as pd
print pd.read_csv('value.txt')
Date price factor_1 factor_2
0 2012-06-11 1600.20 1.255 1.548
1 2012-06-12 1610.02 1.258 1.554
2 2012-06-13 1618.07 1.249 1.552
3 2012-06-14 1624.40 1.253 1.556
4 2012-06-15 1626.15 1.258 1.552
5 2012-06-16 1626.15 1.263 1.558
6 2012-06-17 1626.15 1.264 1.572
This returns pandas DataFrame that is similar to R's
.
这篇关于读取csv文件并在Python中返回data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文