0

I have a dataframe of 100 random numbers and I would like to find the mean as follows:

mean0 should have mean of 0,5,10,... rows

mean1 should have mean of 1,6,11,16,.... rows

.

.

. mean4 should have mean of 4,9,14,... rows.

So far, I am able to find the mean0 but I am not able to figure out a way to iterate the process in order to obtain the remaining means.

My code is as follows:

import numpy as np
import pandas as pd
import csv

data = np.random.randint(1, 100, size=100)
df = pd.DataFrame(data)

print(df)

df.to_csv('example.csv', index=False)

df1 = df[::5]
print("Every 12th row is:\n",df1)

df2 = df1.mean()
print(df2)
1

2 Answers 2

2

Since df[::5] is equivalent to df[0::5], you could use df[1::5], df[2::5], df[3::5], and df[4::5] for the remaining dataframes with subsequent application of mean by df[i::5].mean().

It is not explicitly showcased in the Pandas documentation examples but identical list slicing with [start:stop:step].

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

Comments

1

I would use the underlying numpy array:

df[0].to_numpy().reshape(-1, 5).mean(0)

output: array([40.8 , 52.75, 43.2 , 55.05, 47.45])

5 Comments

This gives me an error: raise KeyError(key) from err KeyError: 0 I don't understand why. Can you please help me?
Obviously, you should use the name of your column. In the provided example this was 0. If the name is 'col' -> df['col']...
When I tried to run the command with the code given above, it gives me the desired results but when I used the same command with df= pd.read_csv('example.csv'), it gives me the error. I haven't changed the name of the column in both the cases. Can you help me to understand what is wrong here?
Use print(df.columns) to see the column names. If you have 0 use df[0], for '0' use df['0']. If you have a single column you can use positional indexing: df.iloc[:,0]. This is all basic pandas and had little to do with how my solution works. The pre-requisite is that you are able to select your column, and that the column contains a multiple of 5 elements.
Thanks! I renamed the column and it worked perfectly for me. Thank you once again.

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.