如何访问由空格包围的CSV列?

时间:2019-06-08 13:05:27

标签: python pandas csv dataframe data-cleaning

我从中导入了一个.cvs文件。 我使用了熊猫数据框。

timestamp ,ty,la,lo,he,acc,v,be,x,y,z
1434838676097.07,gps,48.77,-81.3838208,220.8674103,6,41.72777754,134.6484375,

我已经尽一切努力使它正常工作,但是我无法访问"timestamp"列。我尝试过

d = [0.0, 1.0, 2.0]
e = pd.Series(d, index = ['a', 'b', 'c'])
df = pd.DataFrame({'A': 1., 'B': e, 'C': pd.Timestamp('20130102')})

df.B[0] # 0.0 - fall back to position based
df.B['0'] # KeyError - no label '0' in index
df.B['a'] # 0.0 - found label 'a' in index
df.B.loc[0] # TypeError - string index queried by integer value
df.B.loc['0'] # KeyError - no label '0' in index
df.B.loc['a'] # 0.0 - found label 'a' in index
df.B.iloc[0] # 0.0 - position based query for row 0
df.B.iloc['0'] # TypeError - string can't be used for position
df.B.iloc['a'] # TypeError - string can't be used for position

1 个答案:

答案 0 :(得分:3)

您可以“以编程方式”进行操作:

In [25]: df = pd.read_csv(r'C:\temp\1.csv', skipinitialspace=True)

In [26]: df.columns
Out[26]: Index(['timestamp ', 'ty', 'la', 'lo', 'he', 'acc', 'v', 'be', 'x', 'y', 'z'], dtype='object')
#   NOTE:  ---------------^    

修复:

In [27]: df.columns = df.columns.map(str.strip)

检查:

In [28]: df.columns
Out[28]: Index(['timestamp', 'ty', 'la', 'lo', 'he', 'acc', 'v', 'be', 'x', 'y', 'z'], dtype='object')
#   NOTE:  ---------------^