I have a following dataframe:
import pandas as pd
import itertools
d = {'A': range(1, 11), 'C': range(1, 16)}
df = pd.DataFrame(itertools.product(*d.values()),columns=d.keys())
I would like to add new column Gc
which will take a value of two nested min:
df.assign(Gc=min(53*df["C"] - 0.9*df["C"]**2, min(62*df["A"] - 1.5*df["A"]**2, 15)))
But I got and error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
What is the problem here, please?
CodePudding user response:
The issue is that min
is a python function, which only works with scalar (not vectors).
You can use numpy:
import numpy as np
df.assign(Gc=np.minimum(53*df["C"] - 0.9*df["C"]**2,
np.minimum(62*df["A"] - 1.5*df["A"]**2, 15)))
output:
A C Gc
0 1 1 15.0
1 1 2 15.0
2 1 3 15.0
3 1 4 15.0
4 1 5 15.0
.. .. .. ...
145 10 11 15.0
146 10 12 15.0
147 10 13 15.0
148 10 14 15.0
149 10 15 15.0
[150 rows x 3 columns]