欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/3/21 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 统计123出现次数_如何使用 count 统计词条出现次数? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

  如何获取数据最多的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"]]

总结

以上是生活随笔为你收集整理的统计123出现次数_如何使用 count 统计词条出现次数?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。