Compute number of floats in a int range - Python

Viewed 32

I've the following dataframe containing floats as input and would like to compute how many values are in range 0;90 and 90;180. The output dataframe was obtained using frequency() function from excel.

[Input dataframe]

1

[Desired output]

2

I'd like to do the same thing with python but didn't find a solution. Do you have any suggestion ?

I can also provide source files if needed.

1 Answers

Here's one way, by dividing the columns by 90, then using groupy and count:

import numpy as np
import pandas as pd

data = [
        [87.084,5.293],
        [55.695,0.985],
        [157.504,2.995],
        [97.701,179.593],
        [97.67,170.386],
        [118.713,177.53],
        [99.972,176.665],
        [124.849,1.633],
        [72.787,179.459]
    ]

df = pd.DataFrame(data,columns=['Var1','Var2'])
df = (df / 90).astype(int)
df1 = pd.DataFrame([["0-90"], ["90-180"]])
df1['Var1'] = df.groupby('Var1').count()
df1['Var2'] = df.groupby('Var2').count()
print(df1)

Output:

        0  Var1  Var2
0    0-90     3     4
1  90-180     6     5
Related