-1

I have a data frame like the one below.

amplitude   -13.125 |-13.125 |-11.875 |-11.875 |-11.25  |-11.25
duration -----------|--------|--------|--------|--------|--------
1           NaN     |NaN     |NaN     |NaN     |NaN     |NaN
2           NaN     |0.008032|NaN     |NaN     |NaN     |NaN
3           0.004016|NaN     |NaN     |NaN     |0.004016|0.004016
4           0.9     |NaN     |NaN     |NaN     |NaN     |NaN
5           NaN     |NaN     |NaN     |NaN     |NaN     |NaN
--------------------|--------|--------|--------|--------|--------
sum         0.904016|0.008032|NaN     |NaN     |0.004016|0.004016

How do I find the value at the intersection of the row and column in the data frame? Also, I want calculate the density by dividing the value I found by the value in 'sum'. Example:

duration        amplitude       density 
3               -13.125      0.004016/0.904016  
2               -13.125      0.008032/0.008032
... 

Uğur Eren
  • 163
  • 3
  • 11

1 Answers1

0

Assumming the summation line is the last line of your dataframe:

# Divide every line except the last line by the last line
density = (df.iloc[:-1] / df.iloc[-1]).stack().to_frame('density')

Result:

                     density
duration amplitude          
2        -13.125    1.000000
3        -13.125    0.004442
         -11.250    1.000000
         -11.250    1.000000
4        -13.125    0.995558
Code Different
  • 90,614
  • 16
  • 144
  • 163