pandas 从 Python 中的 Dataframe 过滤多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40510820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:24:18  来源:igfitidea点击:

Filtering multiple conditions from a Dataframe in Python

pythonpandasdataframe

提问by Deepak M

I want to filter out data from a dataframe using multiple conditions using multiple columns. I tried doing so like this:

我想使用多列使用多个条件从数据框中过滤掉数据。我试着这样做:

arrival_delayed_weather = [[[flight_data_finalcopy["ArrDelay"] > 0]] & [[flight_data_finalcopy["WeatherDelay"]>0]]]
arrival_delayed_weather_filter = arrival_delayed_weather[["UniqueCarrier", "AirlineID"]]
print arrival_delayed_weather_filter

However I get this error message:

但是我收到此错误消息:

TypeError: unsupported operand type(s) for &: 'list' and 'list'

类型错误:& 的操作数类型不受支持:“列表”和“列表”

How do I solve this?

我该如何解决这个问题?

Thanks in advance

提前致谢

回答by jezrael

You need ()instead []:

您需要()改为[]

arrival_delayed_weather = (flight_data_finalcopy["ArrDelay"] > 0) & 
                           (flight_data_finalcopy["WeatherDelay"]>0)

But it seems you need ixfor selecting columns UniqueCarrierand AirlineIDby mask- a bit modified boolean indexing:

但似乎你需要ix的选择列UniqueCarrier,并AirlineID通过mask-有点修改boolean indexing

mask = (flight_data_finalcopy["ArrDelay"] > 0) & 
        (flight_data_finalcopy["WeatherDelay"]>0)
arrival_delayed_weather_filter=flight_data_finalcopy.ix[mask, ["UniqueCarrier","AirlineID"]]

Sample:

样本:

flight_data_finalcopy = pd.DataFrame({'ArrDelay':[0,2,3],
                                      'WeatherDelay':[0,0,6],
                                      'UniqueCarrier':['s','a','w'],
                                      'AirlineID':[1515,3546,5456]})

print (flight_data_finalcopy)
   AirlineID  ArrDelay UniqueCarrier  WeatherDelay
0       1515         0             s             0
1       3546         2             a             0
2       5456         3             w             6

mask = (flight_data_finalcopy["ArrDelay"] > 0) & (flight_data_finalcopy["WeatherDelay"]>0)
print (mask)
0    False
1    False
2     True
dtype: bool

arrival_delayed_weather_filter=flight_data_finalcopy.ix[mask, ["UniqueCarrier","AirlineID"]]
print (arrival_delayed_weather_filter)
  UniqueCarrier  AirlineID
2             w       5456