Last active
January 4, 2023 06:43
-
-
Save danielcs88/a5bf5088acc582718b5e2d7940418278 to your computer and use it in GitHub Desktop.
Pipe Function to several columns
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
| # Instead of repeating yourself | |
| df["started_at"] = pd.to_datetime(df["started_at") | |
| df["ended_at"] = pd.to_datetime(df["ended_at"]) | |
| # Leverage the power of pipe | |
| df.pipe( | |
| lambda x: x.assign( | |
| **{ # Apply same function to both columns in one go | |
| col: pd.to_datetime(x[col], format="%Y-%m-%d %H:%M:%S") | |
| for col in ["started_at", "ended_at"] | |
| } | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment