0

I have started to learn pandas. I am using Jupyter notebook. I have imported the test data file -Weather I read the file using panda. Below i have given the code. While I try to read max value of temperature or any other column. I am getting below error. Can you please help me to solve the issue.

import pandas as pd
df = pd.read_csv("C:\\Users\\XXXXXXX\\Downloads\\delhi-weather-data\\testset.csv")
df['hum'].max()

Error below:-

KeyError                                  Traceback (most recent call last)
//**C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2441             try:
-> 2442                 return self._engine.get_loc(key)
   2443             except KeyError:
            pandas\_libs\index.pyx in     pandas._libs.index.IndexEngine.get_loc()
            pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
            pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
            pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'hum'
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
<ipython-input-9-fed23426605b> in <module>()
----> 1 df['hum'].max()
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   1962             return self._getitem_multilevel(key)
   1963         else:
-> 1964             return self._getitem_column(key)
   1965 
   1966     def _getitem_column(self, key):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   1969         # get column
   1970         if self.columns.is_unique:
-> 1971             return self._get_item_cache(key)
   1972 
   1973         # duplicate columns & possible reduce dimensionality
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   1643         res = cache.get(item)
   1644         if res is None:
-> 1645             values = self._data.get(item)
   1646             res = self._box_item_values(item, values)
   1647             cache[item] = res
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   3588 
   3589             if not isnull(item):
-> 3590                 loc = self.items.get_loc(item)
   3591             else:
   3592                 indexer = np.arange(len(self.items))[isnull(self.items)]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2442                 return self._engine.get_loc(key)
   2443             except KeyError:
-> 2444                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2445 
   2446         indexer = self.get_indexer([key], method=method, tolerance=tolerance)


       pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
          pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
          pandas\_libs\hashtable_class_helper.pxi in     pandas._libs.hashtable.PyObjectHashTable.get_item()
          pandas\_libs\hashtable_class_helper.pxi in   pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'hum'**//

df.head()



datetime_utc    conds   dewptm  fog     hail    heatindexm  hum     precipm     pressurem   rain    snow    tempm   thunder     tornado     vism    wdird   wdire   wgustm  windchillm  wspdm
0   19961101-11:00  Smoke   9.0     0   0   NaN     27.0    NaN     1010.0  0   0   30.0    0   0   5.0     280.0   West    NaN     NaN     7.4
1   19961101-12:00  Smoke   10.0    0   0   NaN     32.0    NaN     -9999.0     0   0   28.0    0   0   NaN     0.0     North   NaN     NaN     NaN
2   19961101-13:00  Smoke   11.0    0   0   NaN     44.0    NaN     -9999.0     0   0   24.0    0   0   NaN     0.0     North   NaN     NaN     NaN
3

@vivekrajagopalan its simple perhaps you might have spaces at the ends in your column names . Just do df.columns = df.columns.str.strip() then try again – Dark 20 hours ago


Thank you so much AFTER RUNNING THE COMMAND df.columns = df.columns.str.strip()

ITS WORKING. df['hum'].min() 4.0 df['hum'].max() 243.0

Thank you so much.

vivek rajagopalan
  • 843
  • 3
  • 13
  • 22
  • Show a sample of the csv you are loading and/or a sample of the dataframe. The error means that the column you want doesn't exist. – Mad Physicist Dec 28 '17 at 05:51
  • Please find the columns names below datetime_utc conds dewptm fog hail heatindexm hum precipm pressurem rain snow tempm thunder tornado vism wdird wdire wgustm windchillm wspdm – vivek rajagopalan Dec 28 '17 at 06:13
  • Please use the edit button below your question to add crucial information like that. Do not use comments for such things. – Mad Physicist Dec 28 '17 at 06:15
  • The reason I asked for a sample of the CSV is that I want to reproduce your error on my machine. If your interpretation was totally accurate, we wouldn't be trying to solve this here now. – Mad Physicist Dec 28 '17 at 06:16
  • 1
    Try to run df.head() and post the result. – Tai Dec 28 '17 at 06:18
  • Added df.head() in the question. – vivek rajagopalan Dec 28 '17 at 06:27
  • @vivek. Please format that as code. – Mad Physicist Dec 28 '17 at 06:38
  • 2
    print(df.columns) and I think you will know why. See https://stackoverflow.com/help/mcve for how to help us understand your question. – Tai Dec 28 '17 at 06:46
  • 1
    @vivekrajagopalan its simple perhaps you might have spaces at the ends in your column names . Just do `df.columns = df.columns.str.strip()` then try again – Bharath M Shetty Dec 28 '17 at 08:38
  • 1
    df.columns = df.columns.str.strip(). This command worked after this i could able to get the records details. – vivek rajagopalan Dec 29 '17 at 05:06
  • Does this answer your question? [KeyError: "None of \[\['', ''\]\] are in the \[columns\]" pandas python](https://stackoverflow.com/questions/51976930/keyerror-none-of-are-in-the-columns-pandas-python) – cottontail Feb 03 '23 at 09:57

0 Answers0