python - 如何计算数据框列中的出现次数?

标签 python pandas dataframe

我想找出哪些电影有 1 条评论? 2 条评论? 3 条评论?并继续 20 条评论?下面是我的代码。我怎样才能做到这一点?我应该对每种电影类型进行分组吗?

bigdataframe.info
Out[98]: 
<bound method DataFrame.info of        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  

请告诉我解决这个问题的最简单方法。到目前为止,我的想法是尝试以评级必须 = 1 的条件进行分组。我试图不惜一切代价避免循环,因为我已经乏味地将所有信息放入一个大数据框中。

最佳答案

我认为你需要count (如果需要省略 NaNs)或 size (不要省略 NaNs):

df1 = df.groupby(['movietitle','user id']).size().reset_index(name='Count')
df1 = df1[df1['Count'] <= 4]
df1['perc'] = df1['Count'] / len(df)
<小时/>
df1 = df.groupby(['movietitle','user id'])['movietitle'].count().reset_index(name='Count')
df1 = df1[df1['Count'] <= 4]
df1['perc'] = df1['Count'] / len(df)

关于python - 如何计算数据框列中的出现次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47116484/

相关文章:

python - 如何比较python opencv2.4中的surf特征

python - 创建分组比例向量而不丢失行

python-3.x - Pandas 数据框列名 : remove special character

python-3.x - 如何执行条件数据框操作?

python - Pandas :过滤值出现在对的两端,但不在组内

python - 运行时错误 : Error creating new content types

python - 从python中的类列表中随机选择x个项目

python - 在 Gurobi/Python 中获取并比较变量值

python - 将一串空格分隔的单词拆分为多行

r - 计算两个数据帧之间的相关性是否需要循环?