我有以下数据框
private player
现在,我需要删除行以获得以下输出。
WorldBlocks
我尝试了以下方法。而且它们似乎都不起作用。
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'])
非常感谢您的帮助!
答案 0 :(得分:3)
astype(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
答案 1 :(得分:2)
另一种方法是将空白字符串更改为真实的NaN
值,然后将how
参数传递给dropna
并使用all
作为值
import numpy as np
df.replace('',np.nan).dropna(subset=['feature1','feature2'],how='all').fillna('')
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