0

So I need this little piece of code to keep dropping rows until the A1 cell is a specific string, I tried this:

while table[0][0] != 'Nº TAD':
    table = table.drop(table.index[0])

but it seems the loop keeps going for more than I want and I have no idea why

Fabricio_A
  • 58
  • 7

2 Answers2

2

You can itereate over rows like this:

for index, row in table.iterrows():
   if row["col_name"] == 'Nº TAD':
        break
   table.drop([index],inplace=True)
  • thanks man it worked! only thing for anyone that might be looking for this answer in the future it should be iterrows instead of iterows – Fabricio_A Dec 07 '20 at 14:07
0

This does what you want. Just exchange the if-check with the string you want to check for. df is your DataFrame.

In[16]: df
Out[16]: 
   0  1  2  3
0  1  2  3  4
1  1  2  3  4
2  1  2  3  4
3  5  6  7  8

In[17]: new_df = df
   ...: for num, row in enumerate(df.values):
   ...:     if row[0] == 5:
   ...:         break
   ...:     else:
   ...:         new_df = new_df.drop(num)
   ...:   
  
In[18]: new_df
Out[18]: 
   0  1  2  3
3  5  6  7  8
Philipp
  • 652
  • 2
  • 10
  • 28