assign()

noun

say it: “uh-SYN” /əˈsaɪn/

A pandas method that gives back a copy of a table with a new or changed column, so it can be one step in a chain.

What does assign() do in pandas?

assign() hands back a new table with a column added or replaced, leaving the original as it was, so it fits in a method chain. The new column's value is often a lambda, which gets the table as it is at that step.

clean = raw.assign(
    temp_c=lambda df: (df["temp_f"] - 32) * 5 / 9
)

In a sentence

  • “assign() added a Celsius column without changing the raw table.”
  • “Inside the chain, assign() used a lambda to work from the table at that step.”

Level: Freelancer · More Python terms at this level

Updated