简体   繁体   English

从熊猫数据框中随机删除多个行,但有例外

[英]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删除xrows ,但某些row除外。

df : 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 : 我尝试了以下操作,目的是删除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 , eg: 我的目标是从df中随机删除xrows ,但某些row除外,例如:

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


Desired Output: 所需输出:

EG: If the number of rows to remove was 4 , with the exception of the row - df.loc[df['Main'] == 'A'] , the output would be: EG:如果要删除的rows数为4 ,则row df.loc[df['Main'] == 'A']除外,输出为:

                  Main             Ing 
0                  A              Apple                      
2                  Z              Cheese                                
etc. 

Use pd.DataFrame.sample 使用pd.DataFrame.sample

df.drop(df[df.Main != 'A'].sample(4).index)

  Main     Ing
0    A   Apple
2    Z  Cheese

Random pick index from 1 : n (PS: index = 0 is the row you want to keep) 1:n中的随机选择索引(PS:索引= 0是您要保留的行)

import random
df.drop(random.sample(range(1, df.shape[0]), 4),0)

Out[212]: 
  Main    Ing
0    A  Apple
4    D  Dough

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM