Dataframe read from CSV file doesn't contain column

Viewed 30

I have a csv file that looks something like this:

996_axis   996_flux
5100.0     0.8693662522326132
5100.5     0.92424271352166
5101.0     0.890126539973385
5101.5     1.3986609534526506
5102.0     0.9314980463849233
5102.500000000001     0.8787443384465012
5103.000000000001     0.938446925998305
5103.500000000001     0.9623687779700495

I am trying to read this in and get the two columns so I can plot it.

d["df_996"] = pd.read_csv('996.csv')
print(d["df_" + '996'].loc[:,"996_axis"])

Yet, trying this out leads to a KeyError:

KeyError                                  Traceback (most recent call last)
File ~/.local/lib/python3.10/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
   3620 try:
-> 3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:

What am I missing here?

1 Answers

pandas.read_csv uses commas as the delimiter by default, so your parsed dataframe doesn't contain a column named "996_axis". To read data that has a different separator, specify one. For example:

d["df_996"] = pd.read_csv(io.StringIO(csv_in), delimiter=r"\s+")
print(d["df_" + '996'].loc[:,"996_axis"])

prints

0    5100.0
1    5100.5
2    5101.0
3    5101.5
4    5102.0
5    5102.5
6    5103.0
7    5103.5
Name: 996_axis, dtype: float64

If your data is tab-delimited, then specify delimiter=r"\t", but \s+ works too (\s+ means one or more whitespace)

Related