Master03 Skywalker
Master03 Skywalker

Reputation: 85

How to calculate mean values from a column from a range of another column

I have a dataframe with two columns Distance(m) and height(m).

I want to calculate the max, min and average height values from an interval of 0.04439 m of distance.

Distance is a continuous series from 0 to 0.81m each 0.00222m with a total of 403 values length.

The aim is to extract 18 values (max min average) of Height from 18 intervals each 0.0439m distance (the continuous distance series between 0 and 0.81m)

Then, create a dataframe (2 columns) of each distance interval and its respectively max min and avg value of height

Distance(m) = [0, 0.0022, 0.0044, .... 0.81 ]
Height(m) = [ 0, 0.1, 0.5, 0.4, 0.9, .... 0.1]

Dataframe

Distance(m) Hauteur(m)
0   0.00000 0.024711
1   0.00222 0.027125
2   0.00444 0.027961
3   0.00592 0.028880
4   0.00814 0.029417
5   0.01036 0.030100
6   0.01184 0.031440
7   0.01406 0.033486
8   0.01628 0.035371
9   0.01702 0.034865
10  0.01850 0.034976
11  0.02072 0.035458
12  0.02220 0.035132
13  0.02442 0.035541
14  0.02516 0.034973
15  0.02738 0.034044
16  0.02886 0.033878
17  0.03108 0.032232
18  0.03256 0.033035
19  0.03478 0.030564
20  0.03700 0.031252
21  0.03848 0.030833
22  0.04070 0.031696
23  0.04144 0.030501
24  0.04366 0.029986
up to 403 values



df3=df1[['Distance(m)', 'Hauteur(m)']]

bins = [0, 0.0439, 0.0878, 0.1317, 0.1756, 0.2195, 0.2634, 0.3073, 0.3512, 0.3951, 0.439, 0.4829, 0.5268, 0.5707, 0.6146, 0.6595, 0.7024, 0.7463, 0.7902]


df3['min'] = pd.cut(df3['Hauteur(m)'].min, bins)
df3['min']

Error shows: Input array must be 1 dimensional

Does anyone have any suggestions that can help me? Thanks!

Upvotes: 2

Views: 545

Answers (1)

Hryhorii Pavlenko
Hryhorii Pavlenko

Reputation: 3910

That's where you have an error: .min.

What could could do instead:

df3['categories'] = pd.cut(df3['Hauteur(m)'], bins)

(df3.groupby('categories')['Distance(m)', 'Hauteur(m)'].agg(
    {'max': 'max', 
     'min': 'min', 
     'average': 'mean'}))

enter image description here

Upvotes: 1

Related Questions