0

I have these columns in my dataframe:

dataframe

I need to find average of all the [Count] values where Day is 1 or 2 and so on.

I've tried that by using key dictionary:

col_dict = dict(zip(df6.Day, df6.Count))

k=2  # Enter Day
#print(k)
for keys in col_dict.keys():
    if keys == k:
        for i in col_dict:
            a=col_dict[keys] where col_dict==k

But I think this code is not correct.

anothernode
  • 5,100
  • 13
  • 43
  • 62
J.Python
  • 1
  • 1
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 28 '18 at 11:23
  • [Please don't post images of code (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael Jun 28 '18 at 11:23
  • I'm new to stackoverflow so this might happen. – J.Python Jun 28 '18 at 11:27
  • Possible duplicate of [Python 3.4 - How to get the average of dictionary values?](https://stackoverflow.com/questions/30687244/python-3-4-how-to-get-the-average-of-dictionary-values) – Ramesh-X Jun 28 '18 at 12:23

1 Answers1

0

Pandas has purpose-built methods for grouping and aggregating data. Here you can use groupby + mean:

res = df6.groupby('Day')['Count'].mean()

There is no need for dictionary conversion or explicit iteration when vectorised operations are available. If you need a dictionary result, you can convert in a separate step:

res_dict = res.to_dict()
jpp
  • 159,742
  • 34
  • 281
  • 339