2

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.

5
  • 1
    df2 = df.groupby("10s_window", as_index=False).mean()? Commented Apr 19, 2020 at 18:56
  • Is it possible to set the mean values in the new_df for that particular row where row index is window value and remaining values for which data is not present to be zero? Commented Apr 20, 2020 at 18:40
  • what do you mean exactly? maybe you could show what the desired output looks like @aravindpulagam Commented Apr 20, 2020 at 21:13
  • I've edited the question with the output required Commented Apr 20, 2020 at 22:29
  • You cannot have 3000 windows with some "particular window that are not present in the initial data frame". This makes no sense. Commented Apr 21, 2020 at 10:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to set the mean values in the new_df for that particular window and remaining values for which data is not present to be zero?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.