mardi 24 octobre 2017

Remove more than one row at random from a pandas dataframe with an exception

I'm attempting to remove x number of rows from a df with an exception of a certain row.

df:

                  Main             Ing 
0                  A              Apple            
1                  B              Bread            
2                  Z              Cheese            
3                  E              Egg            
4                  D              Dough           
5                  X              Pasta  
etc. 

I've attempted the following, with the intention of removing one row:

r = randint(0, df.shape[0])
df.drop(df.index[r])

However, it doesn't seem to do anything.

My goal is to randomly remove x number of rows from df, with the exception of a certain row, e.g:

df.loc[df['Main'] == 'A']


Desired Output:

E.G: If the number of rows to remove was 4, with the exception of the row - df.loc[df['Main'] == 'A'], the output would be:

                  Main             Ing 
0                  A              Apple                      
2                  Z              Cheese                                
etc. 




Aucun commentaire:

Enregistrer un commentaire