Skip to content
Advertisement

Pandas: create two new columns in a dataframe with values calculated from a pre-existing column

I am working with the pandas library and I want to add two new columns to a dataframe df with n columns (n > 0).
These new columns result from the application of a function to one of the columns in the dataframe.

The function to apply is like:

JavaScript

One method for creating a new column for a function returning only a value is:

JavaScript

So, what I want, and tried unsuccesfully (*), is something like:

JavaScript

What the best way to accomplish this could be ? I scanned the documentation with no clue.

**df['column_A'].map(calculate) returns a pandas Series each item consisting of a tuple z, y. And trying to assign this to two dataframe columns produces a ValueError.*

Advertisement

Answer

I’d just use zip:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement