Skip to content

Instantly share code, notes, and snippets.

@sjtalkar
Last active October 18, 2020 23:23
Show Gist options
  • Save sjtalkar/62279587561b62e99d24a109ea11bb63 to your computer and use it in GitHub Desktop.
Save sjtalkar/62279587561b62e99d24a109ea11bb63 to your computer and use it in GitHub Desktop.
Usage of Transform in Pandas
#Create a GroupBy object
df_groups = df.groupby(['Destination City Name', 'Origin State', 'Origin City', 'Unique Carrier'])
new_df = df_groups.agg(Total_Flights = ('Flight Date','count'))
new_df['Count Delayed Flights'] = df_groups.apply(lambda x : x[x['Arrival Delay'] > 0]['Arrival Delay'].count() )
new_df['Delayed Per Origin'] = new_df.groupby(['Destination City Name', 'Origin State', 'Origin City'])['Count Delayed Flights'].transform('sum')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment