Aravind Pulagam
Aravind Pulagam

Reputation: 165

How to calculate average of values of a column for a particular value in another column?

I have a data frame that looks like this.

enter image description here

How can I get the average doc/duration for each window into another data frame?

I need it in the following way

enter image description here

Dataframe should contain only one column i.e mean. If there are 3000 windows then there should be 3000 rows in axis 0 which represent the windows and the mean will contain the average value. If that particular window is not present in the initial data frame the corresponding value for that window needs to be 0.

Upvotes: 2

Views: 1778

Answers (1)

swiss_knight
swiss_knight

Reputation: 7831

Use .groupby() method and then compute the mean:

import pandas as pd

df = pd.DataFrame({'10s_windows': [304, 374, 374, 374, 374, 3236, 3237, 3237, 3237],
'doc/duration': [0.1, 0.1, 0.2, 0.2, 0.12, 0.34, 0.32, 0.44, 0.2]})

new_df = df.groupby('10s_windows').mean()


Which results in:

Results

Source: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html

Upvotes: 1

Related Questions