简体   繁体   中英

Trouble reading CSV data into Pandas dataframe (Python/Pandas)

I'm having some trouble reading some csv data into a pandas data frame. Here's what my data looks like:

C1,            C2,              C3,              C4,            C5,  
5.0010254,     12,            0.37,          1.2672,        2039.5,
5.0499756,     12,            0.37,          1.2672,        2039.5,
5.1000244,     12,            0.37,          1.2672,        2039.5,
5.1500122,     12,            0.37,          1.2672,        2039.5,
5.2,           12,            0.37,          1.2672,        2039.5,
5.2499878,     12,            0.37,          1.2672,        2039.5,
5.2999756,     12,            0.37,          1.2672,        2039.5,
5.3500244,     12,            0.37,          1.2672,        2039.5,
5.4000122,     12,            0.37,          1.2672,        2039.5,
5.45,          12,            0.37,          1.2672,        2039.5,
5.4999878,     12,            0.37,          1.2672,        2039.5,

As you can see, the data is comma delimited, but also has a lot of spaces in it after the comma's. I do not know if this is what is causing me trouble, but if I say:

infl = pd.read_csv('filename.txt', sep=",", header=1, na_values=["-999"])
print infl['C2']

I get the error:

KeyError: 'C2'

I have tried the read_csv command with and without explicitly defining the delimiter without success. Any help is appreciated!

One solution is to pass the skipinitialspace argument, to specify that all whitespace after the delimiter should be ignored:

pd.read_csv('filename.txt', sep=",", header=1, na_values=["-999"], skipinitialspace=True)

See the docstring of read_csv for all possible arguments: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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