データ-サンプル:
choice col1 col2 col3
1 100 110 100
2 110 110 100
5 110 110 100
...
50 100 200 300
になるはずです:
choice col1 col2
1 100 110
2 110 110
5 110 110
...
50 100 200
列の行50を除くすべての値が100の場合、列3を削除しようとしています。
使用しようとしました:
df['col3'].all() == 100
or
df['col3'].any() == 100
どちらの行も結果を出さず、エラーも発生しません
条件がfromとinvertを満たしているかどうかdf.all()
を返すために使用し、invertを使用してその逆を行い、最後に:を使用して列をフィルタリングします。True/False
df.eq()
True
False
~
True
df.loc[]
df.loc[:,~df.eq(100).all()]
id col1 col2
1 100 110
2 110 110
3 110 110
編集:編集ごとに、同様のロジックでカスタム関数を試すことができます:
def myfunc(x,choice):
x=x.set_index('choice')
cond=x.loc[:,'col3'].drop(choice).eq(100).all()
if cond:
return x.drop('col3',1).reset_index()
else:
return x.reset_index()
myfunc(df,'50') #if choice column is an integer myfunc(df,50)
choice col1 col2
0 1 100.0 110.0
1 2 110.0 110.0
2 5 110.0 110.0
3 50 100.0 200.0
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加