How to remove a row from pandas dataframe based on the length of the column values?

后端 未结 5 1965
失恋的感觉
失恋的感觉 2021-01-17 10:16

In the following pandas.DataFframe:

df = 
    alfa    beta   ceta
    a,b,c   c,d,e  g,e,h
    a,b     d,e,f  g,h,k
    j,k     c,k,l  f,k,n
         


        
5条回答
  •  [愿得一人]
    2021-01-17 11:01

    This is the numpy version of @NickilMaveli's answer.

    mask = np.core.defchararray.count(df.alfa.values.astype(str), ',') <= 1
    pd.DataFrame(df.values[mask], df.index[mask], df.columns)
    
      alfa   beta   ceta
    1  a,b  d,e,f  g,h,k
    2  j,k  c,k,l  f,k,n
    

    naive timing

提交回复
热议问题