Selecting rows of a dataframe with multiple strings

Viewed 188

I am trying to select multiple rows of a dataframe with different strings. So I have got a data frame of flight schedules and I am trying to filter for multiple airlines. I tried loc, match etc but nothing really workes... Maybe soemone has a hint I tried this:

import pandas as pd
df = pd.read_csv("flights.tsv", sep="\t")
df["ARRIVAL_DELAY"] = df["DEPARTURE_DELAY"] + df["ACTUAL_DURATION"] - df["PLANNED_DURATION"]
df2 = df.loc[df.ARRIVAL_DELAY > 0, :]
df3 = df.loc[(df.CARRIER == "AA", "WN")]
df3

I got a key exception

1 Answers

What you want to do is this, I suppose:

list_of_strings = [ "AA", "WN"]

df3 = df[df["CARRIER"].isin(list_of_strings)]
Related