如何计算出现在2个或更多数据框列中的出现次数?

时间:2017-11-05 13:36:03

标签: python dataframe multiple-columns

以下是我的问题中的数据。这是一组基于电影评论家的代码。一行=评论者的一次评论。

bigdataframe
Out[43]: 
       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 

类型是动作冒险动画儿童...西方。有大约20种类型,但数据框架并没有全部打印出来。我怎样才能弄清楚哪些评论将他们的电影归类为至少2种类型?这意味着他们说电影属于动作和戏剧两种类型。

由于每个类型都在自己的数据帧列中,我对如何执行此操作感到有点困惑。如果有一个数据帧列,我会简单地使用groupby,因为它可以很好地处理类型和它们的数量。

任何见解都会有所帮助!

编辑:作为示例,您可以看到电影" 0"玩具故事被归类为动画和儿童故事,因为它在两列中都有1。

1 个答案:

答案 0 :(得分:1)

基本上,您只对类型列总和大于1的行感兴趣。

对于所有列,df = df[df.sum(axis=1) > 1]可以自动忽略非数字列。

这里真正的问题是如何仅对类型列求和(因为movie id列似乎也是数字)。

如果你有一个外部的流派列表,你可以使用它,即
df = df[df[['Horror', 'Comedy']].sum(axis=1) > 1]