Mohssen Hassan Kassir
Mohssen Hassan Kassir

Reputation: 31

multiplying rows in a pandas dataframe

I have a dataframe that looks like this:

0        0.000313
1        0.316426
2        0.000313
3        0.004389
4        0.000000
5        0.004389

How can I create a dataframe that multiplies and takes the square root of every 2 rows?

For example, for the first 2 rows, it would do this: sqrt(0.000313*0.316426)

It should like this:

0        0.009951
1        0.001172
2        0.000000

Upvotes: 0

Views: 143

Answers (1)

harpan
harpan

Reputation: 8641

I believe what you need is multiplying odd indexed element with even and take sqrt of them:

print(s)

Output:

0    0.000313
1    0.316426
2    0.000313
3    0.004389
4    0.000000
5    0.004389
Name: val, dtype: float64

And then,

pd.Series(np.sqrt([a*b for a,b in zip(list(s[::2]),list(s[1::2]))]))

Output:

0    0.009952
1    0.001172
2    0.000000
dtype: float64

Upvotes: 2

Related Questions