Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying sqrt function on a column

I have following data frame

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
                'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
                'wins': [11, 8, 10, 15, 11, 6, 10, 4],
                'losses': [5, 8, 6, 1, 5, 10, 6, 12]}

football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses'])
football.set_index(['team', 'year'], inplace=True)

How I can apply sqrt function after I do sum to the columns?

football[['wins', 'losses']].sum(axis=1)
like image 241
Night Walker Avatar asked May 16 '16 14:05

Night Walker


People also ask

How do you take the square root of a column in Python?

To calculate the square root in Python, you can use the built-in math library's sqrt() function.

How do you take the square root of a column in R?

Calculate Square root of a number in R Language – sqrt() Function. sqrt() function in R Language is used to calculate the mathematical square-root of the value passed to it as argument.

How do you find the square root in pandas?

1 Answer. You can use transform() function of pandas with 'sqrt' as 'func' or a lamda function for square root as 'func'.

What is sqrt () function?

The SQRT Function[1] is an Excel Math and Trigonometry function. It will provide the square root of a positive number. The function was introduced in MS Excel 2010.


2 Answers

Just use numpy.sqrt() (see docs) on the resulting pd.Series:

import numpy as np
np.sqrt(football[['wins', 'losses']].sum(axis=1))

But there are of course several ways to accomplish the same result - see below for illustration:

df = pd.DataFrame.from_dict(data={'col_1': np.random.randint(low=1, high=10, size=10), 'col_2': np.random.randint(low=1, high=10, size=10)}, orient='index').T

df['sum'] = df[['col_1', 'col_2']].sum(axis=1)
df['np'] = np.sqrt(df[['col_1', 'col_2']].sum(axis=1))
df['apply'] = df[['col_1', 'col_2']].sum(axis=1).apply(np.sqrt)
df['**'] = df[['col_1', 'col_2']].sum(axis=1) ** .5

   col_1  col_2  sum        np     apply        **
0      8      3   11  3.316625  3.316625  3.316625
1      4      1    5  2.236068  2.236068  2.236068
2      6      2    8  2.828427  2.828427  2.828427
3      4      1    5  2.236068  2.236068  2.236068
4      4      7   11  3.316625  3.316625  3.316625
5      7      4   11  3.316625  3.316625  3.316625
6      5      5   10  3.162278  3.162278  3.162278
7      1      2    3  1.732051  1.732051  1.732051
8      6      6   12  3.464102  3.464102  3.464102
9      5      7   12  3.464102  3.464102  3.464102
like image 100
Stefan Avatar answered Nov 01 '22 16:11

Stefan


I'm a personal fan of the built in pandas.DataFrame.pow (docs here). That way you can get roots of various order (like Stefan's last example).

football[['wins','losses']].sum(axis=1).pow(1./2)
like image 34
Dan R. Avatar answered Nov 01 '22 15:11

Dan R.