我有以下数据框
df = pd.DataFrame([['1','aa','ccc','rere','thth','my desc 1','','my feature2 1'], ['1','aa','fff','flfl','ipip','my desc 2','',''], ['1','aa','mmm','rprp','','','',''], ['2','aa','ccc','rprp','','','my feature1 1',''], ['2','aa','fff','bubu','thth','my desc 3','',''], ['2','aa','mmm','fafa','rtrt','my desc 4','',''], ['3','aa','ccc','blbl','thth','my desc 5','my feature1 2','my feature2 2'], ['3','aa','fff','arar','amam','my desc 6','',''], ['3','aa','mmm','acac','ryry','my desc 7','',''],['4','bb','coco','rere','','','','my feature2 3'], ['4','bb','inin','mimi','rere','my desc 8','',''], ['4','bb','itit','toto','enen','my desc 9','',''], ['4','bb','spsp','glgl','pepe','my desc 10','',''], ['5','bb','coco','baba','mpmp','my desc 11','my feature1 3',''], ['5','bb','inin','rere','','','',''],['5','bb','itit','toto','hrhr','my desc 12','',''], ['5','bb','spsp','glgl','lolo','my desc 13','','']], columns=['foo', 'bar','name_input','value_input','bulb','desc','feature1', 'feature2'])
现在,我需要删除行才能获得以下输出。
df = pd.DataFrame([['1','aa','ccc','rere','thth','my desc 1','','my feature2 1'], ['2','aa','ccc','rprp','','my desc 3','my feature1 1',''], ['3','aa','ccc','blbl','thth','my desc 5','my feature1 2','my feature2 2'], ['4','bb','coco','rere','','my desc 8','','my feature2 3'], ['5','bb','coco','baba','mpmp','my desc 11','my feature1 3','']], columns=['foo', 'bar','name_input','value_input','bulb','desc','feature1', 'feature2'])
我尝试了以下方法。但它们似乎都不起作用。
df= df.dropna(subset=['feature1', 'feature2'])
df.dropna(thresh=5, axis=0, inplace=True)
df= df[df.feature2.notnull()]
df= df[pd.notnull(df[['feature1', 'feature2']])]
非常感谢任何帮助!
最佳答案
astype(bool)
空字符串在 bool 上下文中计算结果为 False
。使用 filter
仅获取以 feature
开头的列。然后使用 astype(bool)
,然后使用 any(axis=1)
df[df.filter(regex='fea').astype(bool).any(1)]
foo bar name_input value_input bulb desc feature1 feature2
0 1 aa ccc rere thth my desc 1 my feature2 1
3 2 aa ccc rprp my feature1 1
6 3 aa ccc blbl thth my desc 5 my feature1 2 my feature2 2
9 4 bb coco rere my feature2 3
13 5 bb coco baba mpmp my desc 11 my feature1 3
为了匹配您的结果,我们可以回填 desc
列
feat = df.filter(regex='feat').astype(bool).any(1)
desc = df.desc.where(df.desc.astype(bool)).bfill()
df.assign(desc=desc)[feat]
foo bar name_input value_input bulb desc feature1 feature2
0 1 aa ccc rere thth my desc 1 my feature2 1
3 2 aa ccc rprp my desc 3 my feature1 1
6 3 aa ccc blbl thth my desc 5 my feature1 2 my feature2 2
9 4 bb coco rere my desc 8 my feature2 3
13 5 bb coco baba mpmp my desc 11 my feature1 3
关于python-3.x - 如何根据条件删除pandas中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60006292/