Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save karpanGit/07488571bd5e855a883554354aa2c2fd to your computer and use it in GitHub Desktop.

Select an option

Save karpanGit/07488571bd5e855a883554354aa2c2fd to your computer and use it in GitHub Desktop.
Pandas assign with lambda function with additional (default) arguments
# simple dataframe with three columns
df = pd.DataFrame({'a': [1, 2, 3, 4, 5],
'b': [5, 4, 3, 2, 1],
'c': ['A', 'B', 'C', 'D', 'E']})
df.assign(**{col: lambda df_, col=col: df[col]*2 for col in ['a', 'b']})
# a b c
# 0 2 10 A
# 1 4 8 B
# 2 6 6 C
# 3 8 4 D
# 4 10 2 E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment