I'm trying to replicate some of R4DS's dplyr exercises using Python's pandas, with the nycflights13.flights dataset. What I want to do is select, from that dataset:
- Columns through
yeartoday(inclusive); - All columns that end with "delay";
- The
distanceandair_timecolumns
In the book, Hadley uses the following syntax:
library("tidyverse")
library("nycflights13")
flights_sml <- select(flights,
year:day,
ends_with("delay"),
distance,
air_time
)
In pandas, I came up with the following "solution":
import pandas as pd
from nycflights13 import flights
flights_sml = pd.concat([
flights.loc[:, 'year':'day'],
flights.loc[:, flights.columns.str.endswith("delay")],
flights.distance,
flights.air_time,
], axis=1)
Another possible implementation:
flights_sml = flights.filter(regex='year|day|month|delay$|^distance$|^air_time$', axis=1)
But I'm sure this is not the idiomatic way to write such DF-operation. I digged around, but haven't found something that fits in this situation from pandas API.