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?

>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.

Leave a Reply