Python 选择不包含多个列的行
我以以下方式设置了一个数据帧:Python 选择不包含多个列的行,python,pandas,Python,Pandas,我以以下方式设置了一个数据帧: header_1 | header_2 | header_3 | header_4 a b NaN NaN b c 9 10 x y NaN 8 如何使用列索引(列的名称更改)选择标头_3和标头_4都不为NaN的行?标题_3和标题_4是整数 感谢您如果可能,在列表检查中定义了多个列不缺少筛选列的值,
header_1 | header_2 | header_3 | header_4
a b NaN NaN
b c 9 10
x y NaN 8
如何使用列索引(列的名称更改)选择标头_3和标头_4都不为NaN的行?标题_3和标题_4是整数
感谢您如果可能,在列表检查中定义了多个列不缺少筛选列的值,每行检查所有
True
s:
cols = ['header_3','header_4']
df = df[df[cols].notnull().all(axis=1)]
print (df)
header_1 header_2 header_3 header_4
1 b c 9.0 10.0
# df[df[['header_3', 'header_4']].notnull().all(axis=1)] # Just to avoid creating a list of cols and calling that.
对于“按最后2列选择”,用于“按位置选择”:
df = df[df.iloc[:, -2:].notnull().all(axis=1)]
也可以通过索引器指定列:
#python count from 0
df = df[df.iloc[:, [2,3]].notnull().all(axis=1)]
# df[df.loc[:, ['header_3', 'header_4']].notnull().all(axis=1)] # or can use loc with direct columns name
或者,如果按位
和
只有两列链接条件,则:
df = df[df['header_3'].notnull() & df['header_4'].notnull()]
同样使用
.dropna
subset = ['header_3', 'header_4']
df.dropna(subset=subset, thresh=len(subset))
# header_1 header_2 header_3 header_4
#1 b c 9.0 10.0
嘿,谢谢你的回答!有没有办法改用列索引?问题中的两列始终是数据框中的最后一列,但它们的名称可能会有所不同change@Macterror-当然,检查编辑的答案。这就成功了!我会在6分钟内接受你的回答。多谢各位much@jezrael,我们也可以添加
df[df.loc[:,['header_3','header_4']].notnull().all(axis=1)]
只是为了演示列名。@pygo-是的,所以像df=df[df['header_3','header_4'].notnull().all(axis=1)]
?