1

I am new to python and learning pandas myself. So I get the result for the first syntax but not for the second syntax. As per my understanding, we are using loc for label. So, we should be able to mention the column name inside the parenthesis. Could you please help me out?

df1['EdLevel'].value_counts()--this gives the results

df1.loc['EdLevel'].value_counts()---gives error while running.

The error is something like this:

    KeyError                                  Traceback (most recent call last)
    ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
       2645             try:
    -> 2646                 return self._engine.get_loc(key)
       2647             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\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()
    
    KeyError: 'EdLevel'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-105-bc8968bf3244> in <module>
----> 1 df1.loc['EdLevel'].value_counts()
4
  • Perhaps you could provide a minimal example that produces the error you get? Commented Jul 4, 2020 at 6:57
  • 1
    @pr94:df1['EdLevel'].value_counts()-using this sytax I get the results as: Master’s degree (MA, MS, M.Eng., MBA, etc.) 19569 Some college/university study without earning a degree 10502 Secondary school (e.g. American high school, German Realschule or Gymnasium, etc.) 8642 Associate degree 2938 but not with loc syntax Commented Jul 4, 2020 at 7:02
  • Yeah sure, but it is difficult to help you without being able to reproduce you code. Commented Jul 4, 2020 at 7:05
  • @pr94 :So how do I paste the screenshot I have. I tried to paste the screen shot of the code with issue along with the text but not able to do both at the same time. It would have been clear if I was able to post the screenshot Commented Jul 4, 2020 at 7:07

1 Answer 1

1

Generally, The first syntax you mentioned is used to acess column. But, you can also acess column using df.loc[] also. It can be done as below.

import pandas as pd
df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
      index=['cobra', 'viper', 'sidewinder'],
      columns=['max_speed', 'shield'])
        max_speed shield
cobra       1       2
viper       4       5
sidewinder  7       8
df.loc[:,'shield'] # acess entire 'shield' column

cobra         2
viper         5
sidewinder    8
Name: shield, dtype: int64

You can also acess multiple columns. df.loc[:,['shield','max_speed']] but you cannot use value_counts() method while acessing multiple columns as it returns dataframe not series.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.