V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fanqieipnet
V2EX  ›  推广

如何使用 count 统计词条出现次数?

  •  
  •   fanqieipnet · 2020-12-03 18:06:37 +08:00 · 515 次点击
    这是一个创建于 1265 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如何获取数据最多的 3 个分类?如何使用 count 统计词条出现次数?今天番茄加速跟大家讲一下。

      如何快速拿到数据最多的 3 个分类?

      读入数据:

       df = pd.read_csv("IMDB-Movie-Data.csv")

       df

       1000 行数据,genre 取值的频次统计如下:

       vc = df["genre"].value_counts()

       vc

      打印结果:

       Action,Adventure,Sci-Fi 50

       Drama 48

       Comedy,Drama,Romance 35

       Comedy 32

       Drama,Romance 31

      ..

       Adventure,Comedy,Fantasy 1

       Biography,History,Thriller 1

       Action,Horror 1

       Mystery,Thriller,Western 1

       Animation,Fantasy 1

       Name: genre, Length: 207, dtype: int64

      筛选出 top3 的 index:

       top_genre = vc[0:3].index

       print(top_genre)

      打印结果:

       Index(['Action,Adventure,Sci-Fi', 'Drama', \

      'Comedy,Drama,Romance'], dtype='object')

      使用得到的 top3 的 index,结合 isin,选择出相应的 df

       df_top = df[df["genre"].isin(top_genre)]

       df_top

      结果:

      如何使用 count 统计词条出现次数?

      读入 IMDB-Movie-Data 数据集,1000 行数据:

       df = pd.read_csv("../input/imdb-data/IMDB-Movie-Data.csv")

       df['Title']

      打印 Title 列:

       0 Guardians of the Galaxy

       1 Prometheus

       2 Split

       3 Sing

       4 Suicide Squad

      ...

       995 Secret in Their Eyes

       996 Hostel: Part II

       997 Step Up 2: The Streets

       998 Search Party

       999 Nine Lives

       Name: Title, Length: 1000, dtype: object

      标题是由几个单词组成,用空格分隔。

       df["words_count"] = df["Title"].str.count(" ") + 1

       df[["Title","words_count"]]
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1052 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:33 · PVG 03:33 · LAX 12:33 · JFK 15:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.