如何从CSV文件读取Pandas系列

s5a0g9ez  于 2022-12-15  发布在  其他
关注(0)|答案(7)|浏览(153)

我有一个CSV文件,格式如下:

somefeature,anotherfeature,f3,f4,f5,f6,f7,lastfeature
0,0,0,1,1,2,4,5

我试着把它当作一个Pandas系列来阅读(使用Python 2.7的Pandas每日快照)。

import pandas as pd
types = pd.Series.from_csv('csvfile.txt', index_col=False, header=0)

以及:

types = pd.read_csv('csvfile.txt', index_col=False, header=0, squeeze=True)

但这两种方法都行不通:第一个函数给出随机结果,第二个函数只是导入一个DataFrame而不进行压缩。
看起来Pandas只能识别为一系列CSV格式如下:

f1, value
f2, value2
f3, value3

但是当特征键在第一行而不是第一列时,Pandas不想挤压它。
我还能试试别的吗?这是故意的吗?

o7jaxewo

o7jaxewo1#

以下是我找到的方法:

df = pandas.read_csv('csvfile.txt', index_col=False, header=0);
serie = df.ix[0,:]

对我来说似乎有点愚蠢,因为Squeeze应该已经这样做了。这是一个bug还是我错过了什么?
/EDIT:执行此操作的最佳方法:

df = pandas.read_csv('csvfile.txt', index_col=False, header=0);
serie = df.transpose()[0] # here we convert the DataFrame into a Serie

这是将面向行的CSV行放入panda Series的最稳定的方法。
顺便说一句,squeeze=True参数现在是无用的,因为截至今天(2013年4月),它只适用于面向行的CSV文件,请参阅官方文档:
http://pandas.pydata.org/pandas-docs/dev/io.html#returning-series

b5lpy0ml

b5lpy0ml2#

这样就可以了。挤压仍然可以,但它不能单独工作。index_col需要设置为0,如下所示

series = pd.read_csv('csvfile.csv', header = None, index_col = 0, squeeze = True)
1tuwyuhd

1tuwyuhd3#

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
fd3cxomn

fd3cxomn4#

ds = pandas.read_csv('csvfile.csv', index_col=False, header=0);    
X = ds.iloc[:, :10] #ix deprecated
gt0wga4j

gt0wga4j5#

Pandas取值逻辑为:

DataFrame -> Series=DataFrame[Column] -> Values=Series[Index]

所以我建议:

df=pandas.read_csv("csvfile.csv")
s=df[df.columns[0]]
tyg4sfes

tyg4sfes6#

from pandas import read_csv

series = read_csv('csvfile.csv', header=0, parse_dates=[0], index_col=0, squeeze=True
i2byvkas

i2byvkas7#

由于上面的答案对我都不起作用,下面是另一个答案,从DataFrame手动重新创建Series。

# create example series
series = pd.Series([0, 1, 2], index=["a", "b", "c"])
series.index.name = "idx"
print(series)
print()

# create csv
series_csv = series.to_csv()
print(series_csv)

# read csv
df = pd.read_csv(io.StringIO(series_csv), index_col=0)
indx = df.index
vals = [df.iloc[i, 0] for i in range(len(indx))]
series_again = pd.Series(vals, index=indx)
print(series_again)

输出:

idx
a    0
b    1
c    2
dtype: int64

idx,0
a,0
b,1
c,2

idx
a    0
b    1
c    2
dtype: int64

相关问题