繁体   English   中英

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

[英]How can I access a CSV column surrounded by whitespaces?

我有一个.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

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

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:  ---------------^    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM