从数据框中的列中提取项目

Extract items from Column in a Dataframe

country_name rank show_title
Argentina 2 The Queen of Flow
India 1 Cobra Kai
Argentina 1 The Queen of Flow
England 3 Stay Close
Argentina 1 The Queen of Flow

我正在尝试获得一个 table 来显示每个节目标题排名第一、第二或第三的次数。结果是这样的:

Rank Cobra Kai The Queen of Flow Stay Close
1 1 2 0
2 0 1 1
3 0 0 0

你可以这样使用pivot_table

df.pivot_table(index=['rank'], columns=['show_title'], aggfunc='count', fill_value=0)

结果

           country_name
show_title    Cobra Kai Stay Close The Queen of Flow
rank
1                     1          0                 2
2                     0          0                 1
3                     0          1                 0