Skip to content

Instantly share code, notes, and snippets.

@danielcs88
Last active January 4, 2023 06:43
Show Gist options
  • Select an option

  • Save danielcs88/a5bf5088acc582718b5e2d7940418278 to your computer and use it in GitHub Desktop.

Select an option

Save danielcs88/a5bf5088acc582718b5e2d7940418278 to your computer and use it in GitHub Desktop.
Pipe Function to several columns
# 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