Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择不包含多个列的行_Python_Pandas - Fatal编程技术网

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)]