我有一个像这样的数据框:
Col0 Col1 Col2 Col3
1 a b g a
2 a d z a
3 a g x a
4 a h p a
5 a b c a
我需要删除值为'a'的列。没有其他单元格包含值'a'(例如,此处Col1和Col2将没有值为'a'的单元格。)我有大约1000列,我不确定所有列的值是否为'a'。 所需的数据框应该是这样的。
Col1 Col2
1 b g
2 d z
3 g x
4 h p
5 b c
最好的方法是什么?
答案 0 :(得分:4)
如果需要检查至少有一个True
或any
是否需要检查all
和loc
的所有print (df)
Col0 Col1 Col2 Col3
0 a a g a
1 a d z a
2 a g x a
3 a h p a
4 a b c a
df2 = df.loc[:, ~(df == 'a').any()]
print (df2)
Col2
0 g
1 z
2 x
3 p
4 c
df1 = df.loc[:, ~(df == 'a').all()]
print (df1)
Col1 Col2
0 a g
1 d z
2 g x
3 h p
4 b c
,请使用boolean indexing
,因为过滤列:
print (df == 'a')
Col0 Col1 Col2 Col3
0 True True False True
1 True False False True
2 True False False True
3 True False False True
4 True False False True
详情:
df2 = df.loc[:, (df != 'a').any()]
print (df2)
Col1 Col2
0 a g
1 d z
2 g x
3 h p
4 b c
df1 = df.loc[:, (df != 'a').all()]
print (df1)
Col2
0 g
1 z
2 x
3 p
4 c
print (df != 'a')
Col0 Col1 Col2 Col3
0 False False True False
1 False True True False
2 False True True False
3 False True True False
4 False True True False
string
编辑:
对于检查混合类型 - 带字符串的数字是2种可能的解决方案将所有转换为df.astype(str) == 'a'
或比较numpy数组:
df.values == 'a'
或者:
"@angular/material": "2.0.0-beta.10"
答案 1 :(得分:3)
选项1
将pd.DataFrame.dropna
与pd.DataFrame.mask
一起使用
我的概念是将'a'
替换为np.nan
,然后方便地使用dropna
。
即使它有一个a
,也会删除该列。
df.mask(df.astype(str).eq('a')).dropna(1)
Col1 Col2
1 b g
2 d z
3 g x
4 h p
5 b c
这要求列的所有元素都是a
df.mask(df.astype(str).eq('a')).dropna(1, how='all')
Col1 Col2
1 b g
2 d z
3 g x
4 h p
5 b c
选项2
使用np.where
创建方式查找具有'a'
的唯一列位置
这很酷,因为np.where
将返回一个数组元组,这些数组给出数组中所有True
值的位置。元组的第二个数组将是所有列位置。我抓住了一组独特的,并找到其他列名。
df[df.columns.difference(
df.columns[np.unique(np.where(df.astype(str).eq('a'))[1]
)])]
Col1 Col2
1 b g
2 d z
3 g x
4 h p
5 b c
或类似于pd.DataFrame.drop
df.drop(df.columns[np.unique(np.where(df.astype(str).eq('a'))[1])], 1)
Col1 Col2
1 b g
2 d z
3 g x
4 h p
5 b c
选项3
这可能是糟糕的做法。
df.loc[:, ~df.astype(str).sum().str.contains('a')]
Col1 Col2
1 b g
2 d z
3 g x
4 h p
5 b c