I want to drop some rows with does match with if conditions in my code, but it can not , it pop out the error as below , could you please help assist ?
def listconvert(df_test):
df1 = []
for i in df_test:
if str(i) != 'nan':
df1.append(i)
return df1
for j in range(len(df)-1):
# print(len(listconvert(df.iloc[j].values)) )
if len(listconvert(df.iloc[j].values)) < len(df.columns):
# df.drop(df.loc[df.index==j].index,inplace=True)
print(len(listconvert(df.iloc[j].values)))
df = df.drop(df.index[j])
else:
pass
# df1.to_excel("Mar-131.xlsx")
the error as below:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16140/2461102218.py in <module>
45 for j in range(len(df)-1):
46 # print(len(listconvert(df.iloc[j].values)) )
---> 47 if len(listconvert(df.iloc[j].values)) < len(df.columns):
48 # df.drop(df.loc[df.index==j].index,inplace=True)
49 print(len(listconvert(df.iloc[j].values)))
IndexError: single positional indexer is out-of-bounds
CodePudding user response:
You should rethink your code. Fundamentally all it's doing is dropping rows which contain one or more NANs. You can do that 1000x faster like this:
df.dropna(inplace=True)
Or, just for inspiration to show how you could do it yourself if there weren't a prebuilt function for it:
df = df[df.notna().all(axis=1)]
That's it, that replaces all the code you had, with no slow for
loops.