[英]Removing a column in a pandas dataframe if all the values are a certain string
I have the following dataframe called df.我有以下 dataframe 称为 df。
Sometimes it looks like this (where each value is unique)有时看起来像这样(每个值都是唯一的)
key value1 value2 value3
0 key1 value value value
1 key2 value value value
Sometimes the entire column of value3
are is filled with dashes: '-'
.有时value3
的整个列都用破折号填充: '-'
。
key value1 value2 value3
0 key1 value value -
1 key2 value value -
I want to find a command which will drop the column value3 if all the items in the column dashes.我想找到一个命令,如果列中的所有项目都破折号,它将删除列 value3。
I tried using df['value3'].any()
and that that returns a '-'
.我尝试使用df['value3'].any()
并返回一个'-'
。
Is the right way to do what I want this?做我想做的事情的正确方法是什么?
if df['value3'].any() == '-':
df = df.drop['value3']
Or is there a better way?或者,还有更好的方法?
Use利用
>>> df.loc[:, ~df.eq('-').all()]
key value1 value2
0 key1 value value
1 key2 value value
Compare for not equal by DataFrame.ne
and then get all columns with match by DataFrame.any
in boolean indexing
:通过DataFrame.ne
比较不相等,然后在boolean indexing
中获取与DataFrame.any
匹配的所有列:
print (df.loc[:, df.ne('-').any()])
key value1 value2
0 key1 value value
1 key2 value value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.