将 CSV 文件作为 Pandas DataFrame 导入 [英] Import CSV file as a pandas DataFrame
本文介绍了将 CSV 文件作为 Pandas DataFrame 导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将 CSV 文件读入 pandas DataFrame 的 Python 方法是什么(然后我可以将其用于统计操作,可以具有不同类型的列等)?
What's the Python way to read in a CSV file into a pandas DataFrame (which I can then use for statistical operations, can have differently-typed columns, etc.)?
我的 CSV 文件 "value.txt"
有以下内容:
My CSV file "value.txt"
has the following content:
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 中,我们将使用以下方式读取此文件:
In R we would read this file in using:
price <- read.csv("value.txt")
这将返回一个 R data.frame:
and that would return an R data.frame:
> 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
这将返回与 DataFrame>R 的
.
This returns pandas DataFrame that is similar to R's
.
这篇关于将 CSV 文件作为 Pandas DataFrame 导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文