1

I have a dataframe of 300 columns, I'm trying to calculate the mean of the columns in this dataframe, but I don't know how to change the columns.

count=0
for i in range(0,len(df)):
  count=count+1
  mean = statistics.mean(df.A1)
  print('C',count, " ", mean, sep= "") 

I need to get the column names from A1 to A300 row by row. I didn't want to do this manually

My dataframe columns like A1,A2,A3.. A.300

how can i step by step change this columns name?

2
  • I'm trying to calculate the mean of the columns in this dataframe: have you tried df.mean(), no need to iterate. Commented Aug 1, 2021 at 19:09
  • Does this answer your question? pandas get column average/mean Commented Aug 1, 2021 at 19:11

1 Answer 1

1

Just iterate through columns,

cols = df.columns
for col in cols:
  mean = df[col].mean()
  print(col, mean) 
Sign up to request clarification or add additional context in comments.

6 Comments

Not work: ` TypeError: can't convert type 'str' to numerator/denominator ` @RG_RG
can you share your dataframe?
If you just want mean, you can use df.mean() function.
I don't need a function, i need a loop for the every columns mean. Like. A1 means, A2 means... A300 means. so i need change the column name. But i don't know i make this.
df.mean() is an inbuilt method. just use it like the code above, it works.
|

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.