Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created May 20, 2021 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/156d48ee05fcbe8d3f8b122c013262fc to your computer and use it in GitHub Desktop.
Save derrickturk/156d48ee05fcbe8d3f8b122c013262fc to your computer and use it in GitHub Desktop.
are Pandas users masochists or idiots?
import numpy as np
import pandas as pd
# our goal is to standardize exactly 2 out of 4 columns in a grouped frame,
# using the within-group standard deviation and mean
df = pd.DataFrame({
'a': ['Bennie', 'Bennie', 'Bennie', 'The Jets', 'The Jets'],
'b': [1.0, 3.0, 2.5, 7.1, 8.9],
'c': [-71.3, -80.4, -68.1, 2.5, 3.4],
'd': [25, 30, 40, 10, 15] # don't standardize me!
})
df[['b', 'c']] = df.groupby('a')[['b', 'c']].transform(
lambda x: (x - x.mean()) / x.std())
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment