pandas 如何从 CSV 文件中读取熊猫系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15760856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a pandas Series from a CSV file
提问by gaborous
I have a CSV file formatted as follows:
我有一个格式如下的 CSV 文件:
somefeature,anotherfeature,f3,f4,f5,f6,f7,lastfeature
0,0,0,1,1,2,4,5
And I try to read it as a pandas Series (using pandas daily snapshot for Python 2.7). I tried the following:
我尝试将其作为熊猫系列阅读(使用 Python 2.7 的熊猫每日快照)。我尝试了以下方法:
import pandas as pd
types = pd.Series.from_csv('csvfile.txt', index_col=False, header=0)
and:
和:
types = pd.read_csv('csvfile.txt', index_col=False, header=0, squeeze=True)
But both just won't work: the first one gives a random result, and the second just imports a DataFrame without squeezing.
但两者都不起作用:第一个给出随机结果,第二个只是导入一个 DataFrame 而不挤压。
It seems like pandas can only recognize as a Series a CSV formatted as follows:
似乎熊猫只能将 CSV 格式识别为系列,如下所示:
f1, value
f2, value2
f3, value3
But when the features keys are in the first row instead of column, pandas does not want to squeeze it.
但是当 features 键在第一行而不是第一列时,pandas 不想挤压它。
Is there something else I can try? Is this behaviour intended?
还有什么我可以尝试的吗?这种行为是有意的吗?
回答by gaborous
Here is the way I've found:
这是我找到的方法:
df = pandas.read_csv('csvfile.txt', index_col=False, header=0);
serie = df.ix[0,:]
Seems like a bit stupid to me as Squeeze should already do this. Is this a bug or am I missing something?
对我来说似乎有点愚蠢,因为 Squeeze 应该已经这样做了。这是一个错误还是我错过了什么?
/EDIT: Best way to do it:
/编辑:最好的方法:
df = pandas.read_csv('csvfile.txt', index_col=False, header=0);
serie = df.transpose()[0] # here we convert the DataFrame into a Serie
This is the most stable way to get a row-oriented CSV line into a pandas Series.
这是将面向行的 CSV 行转换为 Pandas 系列的最稳定方法。
BTW, the squeeze=True argument is useless for now, because as of today (April 2013) it only works with row-oriented CSV files, see the official doc:
顺便说一句,squeeze=True 参数现在没用,因为截至今天(2013 年 4 月)它仅适用于面向行的 CSV 文件,请参阅官方文档:
http://pandas.pydata.org/pandas-docs/dev/io.html#returning-series
http://pandas.pydata.org/pandas-docs/dev/io.html#returning-series
回答by waitingkuo
In [28]: df = pd.read_csv('csvfile.csv')
In [29]: df.ix[0]
Out[29]:
somefeature 0
anotherfeature 0
f3 0
f4 1
f5 1
f6 2
f7 4
lastfeature 5
Name: 0, dtype: int64
回答by Lucas Mendes Mota Da Fonseca
ds = pandas.read_csv('csvfile.csv', index_col=False, header=0);
X = ds.iloc[:, :10] #ix deprecated