查找多列数据框中的平均值?

时间:2017-11-06 02:38:26

标签: python pandas dataframe average

这是我的数据框:是的,它非常大。

bigdataframe
Out[2]: 
       movie id                              movietitle  releasedate  \
0             1                        Toy Story (1995)  01-Jan-1995   
1             4                       Get Shorty (1995)  01-Jan-1995   
2             5                          Copycat (1995)  01-Jan-1995   
3             7                   Twelve Monkeys (1995)  01-Jan-1995   
4             8                             Babe (1995)  01-Jan-1995   
5             9                 Dead Man Walking (1995)  01-Jan-1995   
6            11                    Seven (Se7en) (1995)  01-Jan-1995   
7            12              Usual Suspects, The (1995)  14-Aug-1995   
8            15               Mr. Holland's Opus (1995)  29-Jan-1996   
9            17              From Dusk Till Dawn (1996)  05-Feb-1996   
10           19                   Antonia's Line (1995)  01-Jan-1995   
11           21           Muppet Treasure Island (1996)  16-Feb-1996   
12           22                       Braveheart (1995)  16-Feb-1996   
13           23                      Taxi Driver (1976)  16-Feb-1996   
14           24              Rumble in the Bronx (1995)  23-Feb-1996   
15           25                    Birdcage, The (1996)  08-Mar-1996   
16           28                        Apollo 13 (1995)  01-Jan-1995   
17           30                    Belle de jour (1967)  01-Jan-1967   
18           31                     Crimson Tide (1995)  01-Jan-1995   
19           32                            Crumb (1994)  01-Jan-1994   
20           42                           Clerks (1994)  01-Jan-1994   
21           44                Dolores Claiborne (1994)  01-Jan-1994   
22           45              Eat Drink Man Woman (1994)  01-Jan-1994   
23           47                          Ed Wood (1994)  01-Jan-1994   
24           48                      Hoop Dreams (1994)  01-Jan-1994   
25           49                             I.Q. (1994)  01-Jan-1994   
26           50                        Star Wars (1977)  01-Jan-1977   
27           54                         Outbreak (1995)  01-Jan-1995   
28           55                Professional, The (1994)  01-Jan-1994   
29           56                     Pulp Fiction (1994)  01-Jan-1994   
        ...                                     ...          ...   
99970       332                   Kiss the Girls (1997)  01-Jan-1997   
99971       334                           U Turn (1997)  01-Jan-1997   
99972       338                             Bean (1997)  01-Jan-1997   
99973       346                     Jackie Brown (1997)  01-Jan-1997   
99974       682  I Know What You Did Last Summer (1997)  17-Oct-1997   
99975       873                  Picture Perfect (1997)  01-Aug-1997   
99976       877                   Excess Baggage (1997)  01-Jan-1997   
99977       886            Life Less Ordinary, A (1997)  01-Jan-1997   
99978      1527                        Senseless (1998)  09-Jan-1998   
99979       272                Good Will Hunting (1997)  01-Jan-1997   
99980       288                           Scream (1996)  20-Dec-1996   
99981       294                        Liar Liar (1997)  21-Mar-1997   
99982       300                    Air Force One (1997)  01-Jan-1997   
99983       310                   Rainmaker, The (1997)  01-Jan-1997   
99984       313                          Titanic (1997)  01-Jan-1997   
99985       322                   Murder at 1600 (1997)  18-Apr-1997   
99986       328                Conspiracy Theory (1997)  08-Aug-1997   
99987       333                        Game, The (1997)  01-Jan-1997   
99988       338                             Bean (1997)  01-Jan-1997   
99989       346                     Jackie Brown (1997)  01-Jan-1997   
99990       354              Wedding Singer, The (1998)  13-Feb-1998   
99991       362              Blues Brothers 2000 (1998)  06-Feb-1998   
99992       683                       Rocket Man (1997)  01-Jan-1997   
99993       689                      Jackal, The (1997)  01-Jan-1997   
99994       690             Seven Years in Tibet (1997)  01-Jan-1997   
99995       748                       Saint, The (1997)  14-Mar-1997   
99996       751              Tomorrow Never Dies (1997)  01-Jan-1997   
99997       879                  Peacemaker, The (1997)  01-Jan-1997   
99998       894                     Home Alone 3 (1997)  01-Jan-1997   
99999       901                        Mr. Magoo (1997)  25-Dec-1997   

       videoreleasedate                                           IMDb URL  \
0                   NaN  http://us.imdb.com/M/title-exact?Toy%20Story%2...   
1                   NaN  http://us.imdb.com/M/title-exact?Get%20Shorty%...   
2                   NaN  http://us.imdb.com/M/title-exact?Copycat%20(1995)   
3                   NaN  http://us.imdb.com/M/title-exact?Twelve%20Monk...   
4                   NaN     http://us.imdb.com/M/title-exact?Babe%20(1995)   
5                   NaN  http://us.imdb.com/M/title-exact?Dead%20Man%20...   
6                   NaN    http://us.imdb.com/M/title-exact?Se7en%20(1995)   
7                   NaN  http://us.imdb.com/M/title-exact?Usual%20Suspe...   
8                   NaN  http://us.imdb.com/M/title-exact?Mr.%20Holland...   
9                   NaN  http://us.imdb.com/M/title-exact?From%20Dusk%2...   
10                  NaN  http://us.imdb.com/M/title-exact?Antonia%20(1995)   
11                  NaN  http://us.imdb.com/M/title-exact?Muppet%20Trea...   
12                  NaN  http://us.imdb.com/M/title-exact?Braveheart%20...   
13                  NaN  http://us.imdb.com/M/title-exact?Taxi%20Driver...   
14                  NaN  http://us.imdb.com/M/title-exact?Hong%20Faan%2...   
15                  NaN  http://us.imdb.com/M/title-exact?Birdcage,%20T...   
16                  NaN  http://us.imdb.com/M/title-exact?Apollo%2013%2...   
17                  NaN  http://us.imdb.com/M/title-exact?Belle%20de%20...   
18                  NaN  http://us.imdb.com/M/title-exact?Crimson%20Tid...   
19                  NaN    http://us.imdb.com/M/title-exact?Crumb%20(1994)   
20                  NaN   http://us.imdb.com/M/title-exact?Clerks%20(1994)   
21                  NaN  http://us.imdb.com/M/title-exact?Dolores%20Cla...   
22                  NaN  http://us.imdb.com/M/title-exact?Yinshi%20Nan%...   
23                  NaN  http://us.imdb.com/M/title-exact?Ed%20Wood%20(...   
24                  NaN  http://us.imdb.com/M/title-exact?Hoop%20Dreams...   
25                  NaN     http://us.imdb.com/M/title-exact?I.Q.%20(1994)   
26                  NaN  http://us.imdb.com/M/title-exact?Star%20Wars%2...   
27                  NaN  http://us.imdb.com/M/title-exact?Outbreak%20(1...   
28                  NaN             http://us.imdb.com/Title?L%E9on+(1994)   
29                  NaN  http://us.imdb.com/M/title-exact?Pulp%20Fictio...   
                ...                                                ...   
99970               NaN  http://us.imdb.com/M/title-exact?Kiss+the+Girl...   
99971               NaN             http://us.imdb.com/Title?U+Turn+(1997)   
99972               NaN       http://us.imdb.com/M/title-exact?Bean+(1997)   
99973               NaN  http://us.imdb.com/M/title-exact?imdb-title-11...   
99974               NaN  http://us.imdb.com/M/title-exact?I+Know+What+Y...   
99975               NaN  http://us.imdb.com/M/title-exact?Picture+Perfe...   
99976               NaN  http://us.imdb.com/M/title-exact?Excess+Baggag...   
99977               NaN  http://us.imdb.com/M/title-exact?Life+Less+Ord...   
99978               NaN  http://us.imdb.com/M/title-exact?imdb-title-12...   
99979               NaN  http://us.imdb.com/M/title-exact?imdb-title-11...   
99980               NaN   http://us.imdb.com/M/title-exact?Scream%20(1996)   
99981               NaN          http://us.imdb.com/Title?Liar+Liar+(1997)   
99982               NaN  http://us.imdb.com/M/title-exact?Air+Force+One...   
99983               NaN  http://us.imdb.com/M/title-exact?Rainmaker,+Th...   
99984               NaN  http://us.imdb.com/M/title-exact?imdb-title-12...   
99985               NaN  http://us.imdb.com/M/title-exact?Murder%20at%2...   
99986               NaN  http://us.imdb.com/M/title-exact?Conspiracy+Th...   
99987               NaN  http://us.imdb.com/M/title-exact?Game%2C+The+(...   
99988               NaN       http://us.imdb.com/M/title-exact?Bean+(1997)   
99989               NaN  http://us.imdb.com/M/title-exact?imdb-title-11...   
99990               NaN  http://us.imdb.com/M/title-exact?Wedding+Singe...   
99991               NaN  http://us.imdb.com/M/title-exact?Blues+Brother...   
99992               NaN  http://us.imdb.com/M/title-exact?Rocket+Man+(1...   
99993               NaN  http://us.imdb.com/M/title-exact?Jackal%2C+The...   
99994               NaN  http://us.imdb.com/M/title-exact?Seven+Years+i...   
99995               NaN  http://us.imdb.com/M/title-exact?Saint%2C%20Th...   
99996               NaN  http://us.imdb.com/M/title-exact?imdb-title-12...   
99997               NaN  http://us.imdb.com/M/title-exact?Peacemaker%2C...   
99998               NaN  http://us.imdb.com/M/title-exact?imdb-title-11...   
99999               NaN  http://us.imdb.com/M/title-exact?imdb-title-11...   

       unknown  Action  Adventure  Animation  Childrens   ...    Western  \
0            0       0          0          1          1   ...          0   
1            0       1          0          0          0   ...          0   
2            0       0          0          0          0   ...          0   
3            0       0          0          0          0   ...          0   
4            0       0          0          0          1   ...          0   
5            0       0          0          0          0   ...          0   
6            0       0          0          0          0   ...          0   
7            0       0          0          0          0   ...          0   
8            0       0          0          0          0   ...          0   
9            0       1          0          0          0   ...          0   
10           0       0          0          0          0   ...          0   
11           0       1          1          0          0   ...          0   
12           0       1          0          0          0   ...          0   
13           0       0          0          0          0   ...          0   
14           0       1          1          0          0   ...          0   
15           0       0          0          0          0   ...          0   
16           0       1          0          0          0   ...          0   
17           0       0          0          0          0   ...          0   
18           0       0          0          0          0   ...          0   
19           0       0          0          0          0   ...          0   
20           0       0          0          0          0   ...          0   
21           0       0          0          0          0   ...          0   
22           0       0          0          0          0   ...          0   
23           0       0          0          0          0   ...          0   
24           0       0          0          0          0   ...          0   
25           0       0          0          0          0   ...          0   
26           0       1          1          0          0   ...          0   
27           0       1          0          0          0   ...          0   
28           0       0          0          0          0   ...          0   
29           0       0          0          0          0   ...          0   
       ...     ...        ...        ...        ...   ...        ...   
99970        0       0          0          0          0   ...          0   
99971        0       1          0          0          0   ...          0   
99972        0       0          0          0          0   ...          0   
99973        0       0          0          0          0   ...          0   
99974        0       0          0          0          0   ...          0   
99975        0       0          0          0          0   ...          0   
99976        0       0          1          0          0   ...          0   
99977        0       0          0          0          0   ...          0   
99978        0       0          0          0          0   ...          0   
99979        0       0          0          0          0   ...          0   
99980        0       0          0          0          0   ...          0   
99981        0       0          0          0          0   ...          0   
99982        0       1          0          0          0   ...          0   
99983        0       0          0          0          0   ...          0   
99984        0       1          0          0          0   ...          0   
99985        0       0          0          0          0   ...          0   
99986        0       1          0          0          0   ...          0   
99987        0       0          0          0          0   ...          0   
99988        0       0          0          0          0   ...          0   
99989        0       0          0          0          0   ...          0   
99990        0       0          0          0          0   ...          0   
99991        0       1          0          0          0   ...          0   
99992        0       0          0          0          0   ...          0   
99993        0       1          0          0          0   ...          0   
99994        0       0          0          0          0   ...          0   
99995        0       1          0          0          0   ...          0   
99996        0       1          0          0          0   ...          0   
99997        0       1          0          0          0   ...          0   
99998        0       0          0          0          1   ...          0   
99999        0       0          0          0          0   ...          0   

       user id  rating  timestamp  age  gender  occupation  zipcode  state  \
0          308       4  887736532   60       M     retired    95076     CA   
1          308       5  887737890   60       M     retired    95076     CA   
2          308       4  887739608   60       M     retired    95076     CA   
3          308       4  887738847   60       M     retired    95076     CA   
4          308       5  887736696   60       M     retired    95076     CA   
5          308       4  887737194   60       M     retired    95076     CA   
6          308       5  887737837   60       M     retired    95076     CA   
7          308       5  887737243   60       M     retired    95076     CA   
8          308       3  887739426   60       M     retired    95076     CA   
9          308       4  887739056   60       M     retired    95076     CA   
10         308       3  887737383   60       M     retired    95076     CA   
11         308       3  887740729   60       M     retired    95076     CA   
12         308       4  887737647   60       M     retired    95076     CA   
13         308       5  887737293   60       M     retired    95076     CA   
14         308       4  887738057   60       M     retired    95076     CA   
15         308       4  887740649   60       M     retired    95076     CA   
16         308       3  887737036   60       M     retired    95076     CA   
17         308       4  887738933   60       M     retired    95076     CA   
18         308       3  887739472   60       M     retired    95076     CA   
19         308       5  887737432   60       M     retired    95076     CA   
20         308       4  887738191   60       M     retired    95076     CA   
21         308       4  887740451   60       M     retired    95076     CA   
22         308       4  887736843   60       M     retired    95076     CA   
23         308       4  887738933   60       M     retired    95076     CA   
24         308       4  887736880   60       M     retired    95076     CA   
25         308       3  887740833   60       M     retired    95076     CA   
26         308       5  887737431   60       M     retired    95076     CA   
27         308       2  887740254   60       M     retired    95076     CA   
28         308       3  887738760   60       M     retired    95076     CA   
29         308       5  887736924   60       M     retired    95076     CA   
       ...     ...        ...  ...     ...         ...      ...    ...   
99970      631       3  888465180   18       F     student    38866     MS   
99971      631       2  888464941   18       F     student    38866     MS   
99972      631       2  888465299   18       F     student    38866     MS   
99973      631       4  888465004   18       F     student    38866     MS   
99974      631       2  888465247   18       F     student    38866     MS   
99975      631       2  888465084   18       F     student    38866     MS   
99976      631       2  888465131   18       F     student    38866     MS   
99977      631       4  888465216   18       F     student    38866     MS   
99978      631       2  888465351   18       F     student    38866     MS   
99979      729       4  893286638   19       M     student    56567     MN   
99980      729       2  893286261   19       M     student    56567     MN   
99981      729       2  893286338   19       M     student    56567     MN   
99982      729       4  893286638   19       M     student    56567     MN   
99983      729       3  893286204   19       M     student    56567     MN   
99984      729       3  893286638   19       M     student    56567     MN   
99985      729       4  893286637   19       M     student    56567     MN   
99986      729       3  893286638   19       M     student    56567     MN   
99987      729       4  893286638   19       M     student    56567     MN   
99988      729       1  893286373   19       M     student    56567     MN   
99989      729       1  893286168   19       M     student    56567     MN   
99990      729       5  893286637   19       M     student    56567     MN   
99991      729       4  893286637   19       M     student    56567     MN   
99992      729       2  893286511   19       M     student    56567     MN   
99993      729       4  893286638   19       M     student    56567     MN   
99994      729       2  893286149   19       M     student    56567     MN   
99995      729       4  893286638   19       M     student    56567     MN   
99996      729       3  893286338   19       M     student    56567     MN   
99997      729       3  893286299   19       M     student    56567     MN   
99998      729       1  893286511   19       M     student    56567     MN   
99999      729       1  893286491   19       M     student    56567     MN   

       State1  
0          CA  
1          CA  
2          CA  
3          CA  
4          CA  
5          CA  
6          CA  
7          CA  
8          CA  
9          CA  
10         CA  
11         CA  
12         CA  
13         CA  
14         CA  
15         CA  
16         CA  
17         CA  
18         CA  
19         CA  
20         CA  
21         CA  
22         CA  
23         CA  
24         CA  
25         CA  
26         CA  
27         CA  
28         CA  
29         CA  
      ...  
99970      MS  
99971      MS  
99972      MS  
99973      MS  
99974      MS  
99975      MS  
99976      MS  
99977      MS  
99978      MS  
99979      MN  
99980      MN  
99981      MN  
99982      MN  
99983      MN  
99984      MN  
99985      MN  
99986      MN  
99987      MN  
99988      MN  
99989      MN  
99990      MN  
99991      MN  
99992      MN  
99993      MN  
99994      MN  
99995      MN  
99996      MN  
99997      MN  
99998      MN  
99999      MN  

所有类型都是: [['行动','冒险','动画''儿童& #39;,'喜剧','犯罪',纪录片','戏剧'幻想',& #39; FilmNoir&#39 ;, '恐怖'音乐','神秘'浪漫'科幻','惊悚& #39;,'战争','西方']]

我如何能够找出哪种类型的平均评价最高,哪些类型的平均评价最低?我应该将评分和所有相应的类型分组?

df = bigdataframe[['Action', 'Adventure','Animation', 'Childrens', 'Comedy', 
                'Crime','Documentary', 'Drama', 'Fantasy', 'FilmNoir', 
                'Horror', 'Musical', 'Mystery', 
                'Romance','SciFi', 'Thriller', 'War', 'Western','rating']]

gp = df.groupby('rating')
result = gp.agg(['mean'])

结果给了我这个:

      Action Adventure Animation Childrens    Comedy     Crime  \
        mean      mean      mean      mean      mean      mean   

评级
1 0.253191 0.131588 0.030442 0.093944 0.372995 0.068249
2 0.286192 0.150308 0.032806 0.084521 0.339138 0.073351
3 0.267232 0.143710 0.037502 0.081709 0.322380 0.073899
4 0.246708 0.129806 0.036051 0.064728 0.284485 0.082958
5 0.240696 0.136928 0.037545 0.057403 0.246403 0.092590

   Documentary     Drama   Fantasy  FilmNoir    Horror   Musical  \
          mean      mean      mean      mean      mean      mean   

评级
1 0.009656 0.289034 0.018331 0.007365 0.082324 0.046645
2 0.005101 0.320756 0.019349 0.008531 0.071592 0.050484
3 0.006042 0.363861 0.016983 0.013520 0.055738 0.052238
4 0.007842 0.427459 0.011207 0.019430 0.047112 0.047609
5 0.009858 0.471534 0.008301 0.026414 0.041366 0.049526

     Mystery   Romance     SciFi  Thriller       War   Western  
        mean      mean      mean      mean      mean      mean  

评级
1 0.041735 0.154173 0.118494 0.203764 0.060065 0.011620
2 0.046262 0.177397 0.133597 0.229903 0.067018 0.015743
3 0.048112 0.186443 0.121422 0.224277 0.074415 0.019893
4 0.056563 0.201381 0.125154 0.222772 0.097589 0.019606
5 0.057780 0.215037 0.137446 0.203387 0.137446 0.018584

1 个答案:

答案 0 :(得分:1)

我认为您需要idxminidxmax,也不需要新的DataFrame,您可以使用bigdataframe并过滤[]中的列:

genres =  ['Action', 'Adventure','Animation', 'Childrens', 'Comedy', 'Crime','Documentary', 'Drama', 'Fantasy', 'FilmNoir', 'Horror', 'Musical', 'Mystery', 'Romance','SciFi', 'Thriller', 'War', 'Western']
df1 = bigdataframe.groupby('rating')[genres].mean()
print (df1)
          Action  Adventure  Animation  Childrens    Comedy     Crime  \
rating                                                                  
1       0.253191   0.131588   0.030442   0.093944  0.372995  0.068249   
2       0.286192   0.150308   0.032806   0.084521  0.339138  0.073351   
3       0.267232   0.143710   0.037502   0.081709  0.322380  0.073899   
4       0.246708   0.129806   0.036051   0.064728  0.284485  0.082958   
5       0.240696   0.136928   0.037545   0.057403  0.246403  0.092590   

        Documentary     Drama   Fantasy  FilmNoir    Horror   Musical  \
rating                                                                  
1          0.009656  0.289034  0.018331  0.007365  0.082324  0.046645   
2          0.005101  0.320756  0.019349  0.008531  0.071592  0.050484   
3          0.006042  0.363861  0.016983  0.013520  0.055738  0.052238   
4          0.007842  0.427459  0.011207  0.019430  0.047112  0.047609   
5          0.009858  0.471534  0.008301  0.026414  0.041366  0.049526   

         Mystery   Romance     SciFi  Thriller       War   Western  
rating                                                              
1       0.041735  0.154173  0.118494  0.203764  0.060065  0.011620  
2       0.046262  0.177397  0.133597  0.229903  0.067018  0.015743  
3       0.048112  0.186443  0.121422  0.224277  0.074415  0.019893  
4       0.056563  0.201381  0.125154  0.222772  0.097589  0.019606  
5       0.057780  0.215037  0.137446  0.203387  0.137446  0.018584  
mingen = df1.idxmin(axis=1).reset_index(name='Genre')
print (mingen)
   rating        Genre
0       1     FilmNoir
1       2  Documentary
2       3  Documentary
3       4  Documentary
4       5      Fantasy

maxgen = df1.idxmax(axis=1).reset_index(name='Genre')
print (maxgen)
   rating   Genre
0       1  Comedy
1       2  Comedy
2       3   Drama
3       4   Drama
4       5   Drama