KeyError Place not found in axis

0 votes

Hi Guys,

I have one DataFrame in pandas as given below.

import pandas as pd
df = pd.read_csv('my.csv')
Place State
0 Kolkata WestBengal
1 Delhi Delhi
2 Bangalore Karnataka
3 Kolkata WestBengal
4 Delhi Delhi

I am trying to drop the Place column from this DataFrame using drop() function. But it is showing me the below error.

KeyError                                  Traceback (most recent call last)
<ipython-input-16-5010dfe7568c> in <module>
----> 1 df.drop('Place',axis=0)
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\ in drop(self, labels, axis, index, columns, level, inplace, errors)
   3995             level=level,
   3996             inplace=inplace,
-> 3997             errors=errors,
   3998         )
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\ in drop(self, labels, axis, index, columns, level, inplace, errors)
   3934         for axis, labels in axes.items():
   3935             if labels is not None:
-> 3936                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   3938         if inplace:
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\ in _drop_axis(self, labels, axis, level, errors)
   3968                 new_axis = axis.drop(labels, level=level, errors=errors)
   3969             else:
-> 3970                 new_axis = axis.drop(labels, errors=errors)
   3971             result = self.reindex(**{axis_name: new_axis})
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\indexes\ in drop(self, labels, errors)
   5015         if mask.any():
   5016             if errors != "ignore":
-> 5017                 raise KeyError(f"{labels[mask]} not found in axis")
   5018             indexer = indexer[~mask]
   5019         return self.delete(indexer)
KeyError: "['Place'] not found in axis"

How can I solve this error?

Jun 24, 2020 in Python by akhtar
• 38,170 points

1 answer to this question.

0 votes


You need to provide the axis parameter in your drop function. By default, it will take axis=0, which means a row-wise operation. So you have to set axis=1 inside drop function to do a column-wise operation.

0 WestBengal
1 Delhi
2 Karnataka
3 WestBengal
4 Delhi

I hope this will help you.

answered Jun 24, 2020 by MD
• 95,060 points
I have the same problem. I set the axis=1, it doesn't help. Still KeyError ... not found in axis.

Can you share your data frame schema and the command you have used to drop?

Related Questions In Python

0 votes
1 answer

Section postgresql not found in the database.ini file

Python doesn't know what $FILEDIR is. Try an absolute path ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,100 points
–1 vote
1 answer

nmap.nmap.PortScannerError: 'nmap program was not found in path

python-nmap module used in python happens to use nmap binary ...READ MORE

answered Jan 30, 2019 in Python by Omkar
• 69,090 points
0 votes
1 answer

Installing package not found in conda

You can install pip in your conda env and then run pip ...READ MORE

answered Jul 31, 2019 in Python by SDeb
• 13,250 points
0 votes
1 answer

Error: "Module not found" while importing tensorflow in pycharm.

Try the following: Go to Files -> Settings ...READ MORE

answered Jun 23, 2020 in Python by Sirajul
• 58,990 points
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,858 views
0 votes
0 answers
0 votes
1 answer

How to delete column from pandas DataFrame?

Hi@akhtar, You can use the del command in ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,060 points
+1 vote
1 answer

ImportError: DLL load failed: The specified module could not be found.

Hi@akhtar, You may get this error if you ...READ MORE

answered Jun 22, 2020 in Python by MD
• 95,060 points