Skip to content

Instantly share code, notes, and snippets.

@banesullivan
Last active September 13, 2019 06:04
Show Gist options
  • Save banesullivan/e265c2999002d5cc23fbc496c38390fa to your computer and use it in GitHub Desktop.
Save banesullivan/e265c2999002d5cc23fbc496c38390fa to your computer and use it in GitHub Desktop.
[Pandas Group By] Example of how I like to use the Pandas group by method. It is particularly helpful when dealing with CSV files of well logs for many different wells where a name/id is available. #pandas
import pandas as pd
# Load pandas data frame
df = pd.read_csv('z-group-by-data.csv')
# Split every well into its own dataframe
# Assumes we have many wells in the same table
# and we want to have a table for each of those
# wells individually.
well_dfs = dict(tuple(df.groupby('well_id')))
# Now use those tables
for well_id, wdf in well_dfs.items():
# Sort by increasing depth
wdf = wdf.sort_values('z')
well_id x y z
A 0.5 1.1 0.0
A 0.5 1.1 1.0
A 0.5 1.1 2.0
A 0.5 1.1 3.0
A 0.5 1.1 4.0
A 0.5 1.1 5.0
A 0.5 1.1 6.0
A 0.5 1.1 7.0
A 0.5 1.1 8.0
A 0.5 1.1 9.0
A 0.5 1.1 10.0
A 0.5 1.1 11.0
A 0.5 1.1 12.0
A 0.5 1.1 13.0
A 0.5 1.1 14.0
A 0.5 1.1 15.0
A 0.5 1.1 16.0
A 0.5 1.1 17.0
A 0.5 1.1 18.0
A 0.5 1.1 19.0
B 5.6 3.8 0.0
B 5.6 3.8 1.0
B 5.6 3.8 2.0
B 5.6 3.8 3.0
B 5.6 3.8 4.0
B 5.6 3.8 5.0
B 5.6 3.8 6.0
B 5.6 3.8 7.0
B 5.6 3.8 8.0
B 5.6 3.8 9.0
B 5.6 3.8 10.0
B 5.6 3.8 11.0
B 5.6 3.8 12.0
B 5.6 3.8 13.0
B 5.6 3.8 14.0
B 5.6 3.8 15.0
B 5.6 3.8 16.0
B 5.6 3.8 17.0
B 5.6 3.8 18.0
B 5.6 3.8 19.0
B 5.6 3.8 20.0
B 5.6 3.8 21.0
B 5.6 3.8 22.0
B 5.6 3.8 23.0
B 5.6 3.8 24.0
B 5.6 3.8 25.0
B 5.6 3.8 26.0
B 5.6 3.8 27.0
B 5.6 3.8 28.0
B 5.6 3.8 29.0
B 5.6 3.8 30.0
B 5.6 3.8 31.0
B 5.6 3.8 32.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment