magicsword
magicsword

Reputation: 1289

Data calculation in pandas python

I have:

    A1   A2  Random data  Random data2  Average    Stddev
0  0.1  2.0          300          3000     1.05  1.343503
1  0.5  4.5         4500           450     2.50  2.828427
2  3.0  1.2          800            80     2.10  1.272792
3  9.0  9.0          900            90     9.00  0.000000

And would like to add a column 'ColumnX' that needs to have the values calculated as :

ColumnX = min(df['Random data']-df['Average'],df[Random data2]-
df[Stddev])/3.0*df['A2'])

I get the error:

ValueError: The truth value of a Series is ambiguous.

Upvotes: 0

Views: 49

Answers (1)

datagarret
datagarret

Reputation: 26

Your error has to do with pandas preferring bitwise operators and using the built in min function isn't going to work row wise.

A potential solution would be to make two new calculated columns then using the pandas dataframe .min method.

df['calc_col_1'] = df['Random data']-df['Average']
df['calc_col_2'] = (df['Random data2']-df['Stddev'])/(3.0*df['A2'])
df['min_col'] = df[['calc_col_1','calc_col_2']].min(axis=1)

The method min(axis=1) will find the min between the two columns by row then assigned to the new column. This way is efficient because you're using numpy vectorization, and it is easier to read.

Upvotes: 1

Related Questions