在python中如何在此数据帧中使用groupby或pivot

在python中如何在此数据帧中使用groupby或pivot,python,pandas,group-by,pivot-table,pandas-groupby,Python,Pandas,Group By,Pivot Table,Pandas Groupby,我有以下数据帧: name state teams score abc NY red 1 def VA yellow 9 ghi MO green 6 abc WA red 2 klm IL yellow 1 ghi MN green 8 def VA blue 3 xyz

我有以下数据帧:

name   state  teams      score

abc    NY      red         1
def    VA      yellow      9
ghi    MO      green       6
abc    WA      red         2
klm    IL      yellow      1
ghi    MN      green       8
def    VA      blue        3
xyz    NY      blue        5
abc    NY      blue        5
abc    NY      red         4
ghi    MN      green       7
我希望以这样的方式对每个名称-状态组合的数据进行分组,我希望每个团队的得分最低,例如在我们拥有的数据中: 名为“abc”、州为“NY”和队为“red”各得1分和4分 “红色”队的最低得分是1分

对于没有得分的球队,最低得分可以是0

示例输出:

name  state   red  yellow  green blue
abc    NY      1    0      0      5
def    VA      0    9      0      3
ghi    MO      0    0      6      0
abc    WA      ....................
klm    IL      ....................
ghi    MN      0    0      7     0     
xyz    NY      0    0     0     5   
选项1:使用
groupby
unstack
使用,
first
获取一个值,使用
unstack
中的
fill\u value
参数将NaN替换为零:

df.groupby(['name','state','teams']).min()['score'].unstack(fill_value=0).reset_index()
输出:

teams name state  blue  green  red  yellow
0      abc    NY     5      0    1       0
1      abc    WA     0      0    2       0
2      def    VA     3      0    0       9
3      ghi    MN     0      8    0       0
4      ghi    MO     0      6    0       0
5      klm    IL     0      0    0       1
6      xyz    NY     5      0    0       0
选项2:使用pd.crosstab 选项3:使用pd.pivot_表 选项1:使用
groupby
unstack
使用,
first
获取一个值,使用
unstack
中的
fill\u value
参数将NaN替换为零:

df.groupby(['name','state','teams']).min()['score'].unstack(fill_value=0).reset_index()
输出:

teams name state  blue  green  red  yellow
0      abc    NY     5      0    1       0
1      abc    WA     0      0    2       0
2      def    VA     3      0    0       9
3      ghi    MN     0      8    0       0
4      ghi    MO     0      6    0       0
5      klm    IL     0      0    0       1
6      xyz    NY     5      0    0       0
选项2:使用pd.crosstab 选项3:使用pd.pivot_表 first()给出了可用的第一个分数,但是,我希望得到最小的分数,而不管分数在列表中的顺序如何dataframe@user9463814为此,请尝试
min()。更新的解决方案。first()给出了可用的第一个分数,但是,我希望得到最低分数,而不管分数在列表中的顺序如何dataframe@user9463814为此,请尝试
min()。更新的解决方案。