Created
May 20, 2021 16:48
-
-
Save derrickturk/156d48ee05fcbe8d3f8b122c013262fc to your computer and use it in GitHub Desktop.
are Pandas users masochists or idiots?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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