Created
July 27, 2025 14:10
-
-
Save karpanGit/07488571bd5e855a883554354aa2c2fd to your computer and use it in GitHub Desktop.
Pandas assign with lambda function with additional (default) arguments
This file contains hidden or 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
| # 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