gpt4 book ai didi

python - 根据最高编号将类别分配给 ID。子类 - Pandas

转载 作者:行者123 更新时间:2023-11-30 22:07:25 24 4
gpt4 key购买 nike

所以实际上我有一个带有一些 videoID 的数据帧,其中有一系列带有子类别的视频,我想分配出现次数最多的类。所以我的数据框看起来像这样,

videoId   postId   class

12234 788 1
12234 789 1
12234 790 3
12234 791 4
12234 792 1
12234 793 4

所以我想要每个这样的 videoId 都有一个像这样的数据帧:

videoId   class
12234 1

由于该 videoId 下出现次数最多的类别为 1(计算子帖子类别)

现在假设如果我在类(class)之间有联系,可以这样说:

videoId   postId   class

1620 34 1
1620 35 1
1620 36 2
1620 37 2

我希望它是这样的:

 videoId  class
1620 1
1620 2

因此,当子类之间存在联系时,我希望所有子类都针对该 videoId 出现。我尝试了多种方法,例如 value_counts()max() 等,但无法找到解决方案。

最佳答案

您可以简单地在 groupby 上应用 mode 并重置索引,即

df.groupby('videoId')['class'].apply(pd.Series.mode).reset_index(level=0)

videoId class
0 1620 1
1 1620 2

关于python - 根据最高编号将类别分配给 ID。子类 - Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com