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
x
4
1
def calculate(x):
2
operate
3
return z, y
4
One method for creating a new column for a function returning only a value is:
JavaScript
1
2
1
df['new_col']) = df['column_A'].map(a_function)
2
So, what I want, and tried unsuccesfully (*), is something like:
JavaScript
1
2
1
(df['new_col_zetas'], df['new_col_ys']) = df['column_A'].map(calculate)
2
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
1
24
24
1
In [1]: from pandas import *
2
3
In [2]: def calculate(x):
4
return x*2, x*3 :
5
:
6
7
In [3]: df = DataFrame({'a': [1,2,3], 'b': [2,3,4]})
8
9
In [4]: df
10
Out[4]:
11
a b
12
0 1 2
13
1 2 3
14
2 3 4
15
16
In [5]: df["A1"], df["A2"] = zip(*df["a"].map(calculate))
17
18
In [6]: df
19
Out[6]:
20
a b A1 A2
21
0 1 2 2 3
22
1 2 3 4 6
23
2 3 4 6 9
24