简体   繁体   English

熊猫-如果大多数情况下具有特定值,如何删除行或列?

[英]Pandas- How to drop a row or column if they have a certain value most of the times?

I have a Dataframe where i have some missing values as "none". 我有一个数据框,其中有一些缺少的值“无”。

import pandas as pd df = pd.DataFrame ({'Category': (['none',''women','kids']), 'Sales': (['none','none','40']), '# of customers': (['30','none','50']) })

I want to remove the rows or columns that have most values as 'none'. 我想删除值最多的行或列为“ none”。 How to do this? 这个怎么做? Thank you 谢谢

1st solution is treat none as character not NaN , the we using eq with sum (if need drop row using sum(axis=1) ) 第一种解决方案不将其视为字符而不是NaN,我们将eqsum一起使用(如果需要使用sum(axis=1)删除row

df.loc[:,df.eq('none').sum().lt(2)]
Out[559]: 
  # of customers Category
0             30     none
1           none    women
2             50     kids

2nd solution is assuming your none as np.nan and using dropna with thresh np.nan解决方案是假设您的都不是np.nan并使用带有thresh dropna

#df=df.replace('none',np.nan)

df.dropna(axis=0,thresh=2)#here thresh is Require that many non-NA values.
Out[563]: 
  # of customers Category Sales
2             50     kids    40

Or: 要么:

df.loc[:,(df=='none').sum()<=1]

Output: 输出:

  # of customers Category
0             30     none
1           none    women
2             50     kids

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

相关问题 Pandas-如何获取另一列中每个对应值的行数 - Pandas- How to get number of times row occurs for each corresponding value in another column Python Pandas-根据给定的窗口并从特定值开始计算特定列的总和 - Python Pandas- Calculate sum of a certain column based on a given window and starting at a certain value pandas:如果组的最后一行具有特定的列值,如何删除组的所有行 - pandas: how to drop all rows of a group if the last row of the group has certain column value Pandas:如何删除重复但保留某些行值的列值 - Pandas: How to drop column values that are duplicates but keep certain row values 计算列在Pandas中包含特定值的次数 - Count how many times a column contains a certain value in Pandas 删除一行,如果它包含熊猫中的某个值 - Drop a row if it contains a certain value in pandas 如何删除Pandas DataFrame某列值为NaN的行 - How to drop rows of Pandas DataFrame whose value in a certain column is NaN Pandas 如果列值根据值出现超过一定次数,则删除行 - Pandas drop row if column value has appeared more than some number of times depending on the value python pandas-在您所在的行上方添加一列的值 - python pandas- adding values of a column above the row you're on 如果任何列包含所述行中的某个值,则在Pandas Dataframe中删除一行 - Drop a row in a Pandas Dataframe if any column contains a certain value, in said row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM