Pandas DataFrame: access to multiple elements not equal =!

I have the following Pandas DataFrame df object. This is a train schedule showing the departure date, planned departure time, and trip.

 import pandas as pd df = Year Month DayofMonth DayOfWeek DepartureTime Train Origin Datetime 1988-01-01 1988 1 1 5 1457 BritishRail Leeds 1988-01-02 1988 1 2 6 1458 DeutscheBahn Berlin 1988-01-03 1988 1 3 7 1459 SNCF Lyons 1988-01-02 1988 1 2 6 1501 BritishRail Ipswich 1988-01-02 1988 1 2 6 1503 NMBS Brussels .... 

Now, let's say I wanted to select all the DeutscheBahn items in the Train column.

I would use

 DB = df[df['Train'] == 'DeutscheBahn'] 

Now, how can I select all trains except DeutscheBahn and British Rails and SNCF. How can I select elements at the same time, and not such?

 notDB = df[df['Train'] != 'DeutscheBahn'] 

and

 notSNCF = df[df['Train'] != 'SNCF'] 

but I'm not sure how to combine them into one team.

 df[df['Train'] != 'DeutscheBahn', 'SNCF'] 

does not work.

+14
source share
2 answers
 df[~df['Train'].isin(['DeutscheBahn', 'SNCF'])] 

isin returns the values ​​in df['Train'] that are in this list, and ~ at the beginning is essentially a not operator.

Another working, but longer syntax:

 df[(df['Train'] != 'DeutscheBahn') & (df['Train'] != 'SNCF')] 
+25
source

I like to use the request method, as it is more clear

 df = df.query("Train not in ['DeutscheBahn', 'British Rails', 'SNCF']") 
0
source

Source: https://habr.com/ru/post/1238843/


All Articles