반응형
OR 문을 사용하여 Pandas 데이터 프레임 필터링
판다 데이터 프레임이 있는데 데이터 프레임의 두 열 값을 기준으로 전체를 필터링하려고 합니다.IBRD 또는 IMF!= 0의 모든 행과 열을 되돌리고 싶습니다.
alldata_balance = alldata[(alldata[IBRD] !=0) or (alldata[IMF] !=0)]
하지만 이것은 나에게 가치 오류를 줍니다.
값 오류:시리즈의 참 값은 모호합니다.a.empty, a.boole(), a.item(), a.any() 또는 a.all()을 사용합니다.
그래서 저는 제가 또는 문을 올바르게 사용하지 않는다는 것을 알고 있는데, 이것을 할 수 있는 방법이 있나요?
문서에서:
또 다른 일반적인 작업은 데이터를 필터링하기 위해 부울 벡터를 사용하는 것입니다.연산자는 다음과 같습니다. | for 또는, & for 및 및 ~는 그렇지 않습니다.괄호를 사용하여 그룹화해야 합니다.
https://pandas.pydata.org/docs/user_guide/indexing.html#boolean-indexing
시도:
alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)]
다음과 같은 방법으로 결과를 얻을 수 있습니다.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
....
....
#use filter with plot
#or
fg=sns.factorplot('Retailer country', data=df1[(df1['Retailer country']=='United States') | (df1['Retailer country']=='France')], kind='count')
fg.set_xlabels('Retailer country')
plt.show()
#also
#and
fg=sns.factorplot('Retailer country', data=df1[(df1['Retailer country']=='United States') & (df1['Year']=='2013')], kind='count')
fg.set_xlabels('Retailer country')
plt.show()
두 가지 기능을 모두 사용할 수 있습니다.or그리고.|메서드 내부:
alldata.query('IBRD!=0 or IMF!=0')
그리고.
alldata.query('IBRD!=0 | IMF!=0')
둘 다 같은 결과를 낳습니다.
|로 조건을 구분할 수 있습니다.
#이렇게
df1 = df[(df['age'] > 25) | (df['gender'] == "남성")]
언급URL : https://stackoverflow.com/questions/29461185/filtering-pandas-dataframe-using-or-statement
반응형