Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does dropna not drop na?

From my understanding, dropna for pandas in python drops rows with an empty entry.

import pandas as pd
the_data = pd.read_csv("EnzymeTrainingData.csv")
the_data.dropna(inplace = True)
seqs = the_data["protein_sequence"]
amino_numeros = seqs.apply(len)
hands = amino_numeros.apply(lambda n : float(n*(n + 1))/2)
the_pH = the_data["pH"]


additive = 14
x_train = []
for i in range(len(the_pH)):
    print(i)
    print(the_pH[i])

But for my code, it says at index 69:
KeyError: 69

Why is this? Is it because I am doing something wrong with the dropna? Or is it something else?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you drop rows like that, pandas does not renumber the index. If you drop the row at index 69, then there will be nothing at index 69. Your range is going by row number, but the the_pH[i] notation does a lookup by index.

You can either redo the index, or you can use iterrows to iterate the rows one by one, or use the_pH.iloc[i] to go by row number.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading