从Python中的Dataframe过滤多个条件

时间:2016-11-09 15:43:15

标签: python pandas dataframe

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

%

但是我收到此错误消息:

  

TypeError:&:'list'和'list'的不支持的操作数类型

我该如何解决这个问题?

提前致谢

1 个答案:

答案 0 :(得分:9)

您需要()而不是[]

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

但似乎您需要ixUniqueCarrier选择列AirlineIDmask - 稍微修改boolean indexing

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

样品:

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