Skip to content

Instantly share code, notes, and snippets.

@arccoder
Created October 2, 2019 22:32
Show Gist options
  • Save arccoder/e22f16631a575f275e22b0642fe32c3a to your computer and use it in GitHub Desktop.
Save arccoder/e22f16631a575f275e22b0642fe32c3a to your computer and use it in GitHub Desktop.
Pandas: Sort within groups
# Load the Pandas libraries with alias 'pd'
import pandas as pd
# Read csv to dataframe
df = pd.read_csv("data.csv")
# Set column types
df['Submission Date'] = pd.to_datetime(df['Submission Date'], format="%Y-%m-%d %H:%M")
print(df)
# Steps:
# 1. Sort 'Project Name' column alphabetically
# 2. Group by 'Project Name'
# 3. Sort within each group by 'Submission Date'
df = df.sort_values(['Project Name'], ascending=True) \
.groupby(['Project Name'], sort=False) \
.apply(lambda x: x.sort_values(['Submission Date'], ascending=True)) \
.reset_index(drop=True)
print(df)
df.to_csv('out.csv', encoding='utf-8', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment