1

I have the following dataframe:

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

I want to group the data in such a way for every name state combination, I want the least score for each team, for example in the data we have: name 'abc', state 'NY' and team 'red' has two scores 1 and 4 then here the least score for team 'red' is 1.

And for the teams we do not have the score the least score can be 0.

Example Output:

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   
user9463814
  • 199
  • 11

1 Answers1

4

Option 1: use groupby and unstack

Use, first to get one value, and fill_value parameter in unstack to replace NaN with zeroes:

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

Output:

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

Option 2: use pd.crosstab

(pd.crosstab([df['name'],df['state']],df['teams'],df['score'],aggfunc='min')\
  .fillna(0)
  .astype(int)
  .reset_index())

Option 3: use pd.pivot_table

(pd.pivot_table(df,'score',['name','state'],'teams',aggfunc='min', fill_value=0)
   .reset_index())
Scott Boston
  • 147,308
  • 15
  • 139
  • 187