0

I am getting the below error:

 **raise KeyError("{} not found in axis".format(labels[mask]))
KeyError: '[18] not found in axis'**

I am trying to drop rows based on conditions and my code is below:

PSE2=PSE1

for i in range(1,len(PSE1)):
    if PSE1.iloc[[i],[1]].values==PSE1.iloc[[i+1],[1]].values or PSE1.iloc[[i],[1]].values==PSE1.iloc[[i-1],[1]].values:
        pass
    else:
        print(str(i))
        print(PSE2.iloc[[i],[1]].values)
        PSE2=PSE2.drop([i],axis=0)

PSE1 and PSE2 are two identical Dataframes.

3
  • 1
    Add a minimal reproducible example please. Commented Jan 17, 2020 at 15:00
  • 1
    When asking questions for code that relies on data, it is important that a minimal example of the data is included in your question, especially if that data is from an external resource. The easier you make it for us to copy and paste from your question (so that we can execute your code and test our solution) the more likely you'll get responses. - minimal reproducible example. Commented Jan 17, 2020 at 15:00
  • Noted. Though the question is not related to the type of data. It is about boundries Commented Jan 17, 2020 at 16:34

1 Answer 1

1

The problem probably comes from the [i+1] in the if condition. In the last step of your loop, i = len(PSE1) - 1 so PSE1.iloc[[i+1],[1]] will not exist

for i in range(1,len(PSE1)):
    if PSE1.iloc[[i],[1]].values==PSE1.iloc[[i+1],[1]].values

To solve this you can replace your ifcondition by :

if PSE1.iloc[[i],[1]].values==PSE1.iloc[[i-1],[1]].values or
i < len(PSE1)-1 and
PSE1.iloc[[i],[1]].values==PSE1.iloc[[i+1],[1]].values:
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.