python pandas:过滤掉有多个条件的行

python pandas: filter out rows with multiple conditions

我需要修复 CSV 文件。当我通过 pandas 阅读它时,它只显示一列,但它有多个。

所以我拆分了专栏:

df = df['test_column'].str.split(' ', expand = True)

得到 168 行。

我更改了列的名称: df.set_axis(list(range(1, 169)), axis = 1, inplace = True)

我目前正在查看每一列,看它是否完全为空,或者它是否具有以下价值:

a = 122 #just a column name
df = df[df[a].notnull()]
print(df[a].to_string())

这里的问题是,即使特定的行是空的,它仍然显示给我。我假设只有空格 (" ")。

那么多条件怎么做呢?

IIUC 首先将所有空字符串或空格替换为缺失值:

#removed ' ', by default plitting by arbitrary space
df = df['test_column'].str.split(expand = True)
#starting columns by 1
df.columns += 1

df = df.replace(r'^\s*$', np.nan, regex=True)

a = 122 #just a column name
df = df[df[a].isna()]
print (df)