Jos*_*osh 37 python filter dataframe pandas
我有一个pandas数据帧,我想根据数据框中两列的值过滤整个df.我想找回IBRD或IMF!= 0的所有行和列.
alldata_balance = alldata[(alldata[IBRD] !=0) or (alldata[IMF] !=0)]
Run Code Online (Sandbox Code Playgroud)
但这给了我一个ValueError
ValueError:Series的真值是不明确的.使用a.empty,a.bool(),a.item(),a.any()或a.all().
所以我知道我没有正确使用或声明,有没有办法做到这一点?
Lia*_*ley 70
来自文档:
另一种常见操作是使用布尔向量来过滤数据.运营商是:| for或,&for and,和for for not.必须使用括号对这些进行分组.
http://pandas.pydata.org/pandas-docs/version/0.15.2/indexing.html#boolean-indexing
尝试:
alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)]
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以执行以下操作来达到您的结果:
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()
Run Code Online (Sandbox Code Playgroud)