如何通过计算 pandas 数据框中的值来创建新系列?
How can I create a new series by calculating the values in my pandas data frame?
我有一个 pandas 数据框,如下所示:
like_count dislike_count dog_video cat_video bird_video other_animal_video animal_category
1000 500 True False False False dog
5500 300 False True False False cat
2300 100 False False True False bird
1100 200 False False False True other
我需要做的是通过计算animal_category中狗、猫、鸟等的数量来创建一个新系列。我的新系列应该是这样的:
animal_names number of animals
dog 50
cat 20
bird 15
other 30
我该怎么做?
另一种选择(来自@Quang Hoang 的回答)是使用 groupby()
output_df = df.groupby(['animal_category'],as_index=False)['like_count'].count().rename(columns={'like_count':'number of animals'})
我有一个 pandas 数据框,如下所示:
like_count dislike_count dog_video cat_video bird_video other_animal_video animal_category
1000 500 True False False False dog
5500 300 False True False False cat
2300 100 False False True False bird
1100 200 False False False True other
我需要做的是通过计算animal_category中狗、猫、鸟等的数量来创建一个新系列。我的新系列应该是这样的:
animal_names number of animals
dog 50
cat 20
bird 15
other 30
我该怎么做?
另一种选择(来自@Quang Hoang 的回答)是使用 groupby()
output_df = df.groupby(['animal_category'],as_index=False)['like_count'].count().rename(columns={'like_count':'number of animals'})