0

Here is an image of the CSV file I am working with, for reference

Matplotlib won't read the column names from the .CSV file.

I am trying to plot weather data with one axis being the date and the other being wind speed. I have used pandas to upload the CSV file into Python, as shown here:

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('Censored')
df

but matplotlib won't read any of the columns after using this code:

x = df['date_time']
y = df['wind_speed']
plt.plot(x,y)

This is is the eror code that I receive:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /opt/anaconda3/lib/python3.9/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:

File /opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()

File /opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'date_time'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Input In [36], in <cell line: 1>()
----> 1 x = df['date_time']
      2 y = df['wind_speed']
      3 fig, ax = plt.subplot(figsize=(10,7))

File /opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py:3505, in DataFrame.__getitem__(self, key)
   3503 if self.columns.nlevels > 1:
   3504     return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
   3506 if is_integer(indexer):
   3507     indexer = [indexer]

File /opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
   3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:
-> 3623     raise KeyError(key) from err
   3624 except TypeError:
   3625     # If we have a listlike key, _check_indexing_error will raise
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)

KeyError: 'date_time'

What is going on?

CWK
  • 33
  • 4
  • 1
    That's not a CSV file. Pandas `read_csv` is a very handy shortcut, but if your data doesn't fit the definition of a CSV, then you need to use something else. Show us the text from your data file and perhaps we can advise you on an import method. – Tim Roberts Apr 04 '23 at 19:27
  • 1
    It could be a CSV, just with a semicolon separator: `df = pd.read_csv('Censored', sep=';')` – tdy Apr 04 '23 at 19:28
  • I added the semicolon separator. Thanks. Not sure why it won't plot a column when I say plt.plot(df['date_time'],df['wind_speed']) – CWK Apr 04 '23 at 19:41
  • If it still throws a KeyError, there are probably spurious spaces. Check `print(df.columns)` to see if there are leading/trailing spaces. – tdy Apr 04 '23 at 19:44

0 Answers0