Skip to content Skip to sidebar Skip to footer

Pandas Bring Data From One Df To Another Issue

I have a data set that has a count of duplicates: #Count number of injuries by levels for each players levelcount = df.groupby(['Relinquished','Severity']).size().reset_index(name=

Solution 1:

Use Series.map with dictionary for new column, append to index by DataFrame.set_index and reshape by Series.unstack:

levelcount = df.groupby(['Relinquished','Severity']).size().reset_index(name='count')
d = {1:'DTD',2:'DNP',3:'outindefinitely',4:'outforseason'}

new = levelcount.set_index(levelcount['Severity'].map(d), append=True)['Count'].unstack()
levelcount = levelcount.join(new.reindex(list(d.values()), axis=1))
print (levelcount)
  Relinquished  Severity  Count  DTD  DNP  outindefinitely  outforseason
0      player1         111.0  NaN              NaN           NaN
1      player1         31  NaN  NaN              1.0           NaN
2      player2         31  NaN  NaN              1.0           NaN
3      player3         133.0  NaN              NaN           NaN

Your solution is possible by loop by dictionary and set new columns:

levelcount = df.groupby(['Relinquished','Severity']).size().reset_index(name='count')
d = {1:'DTD',2:'DNP',3:'outindefinitely',4:'outforseason'}

fork, v in d.items():
    levecount = levelcount.loc[levelcount['Severity'] == k, v] = levelcount['count']

Post a Comment for "Pandas Bring Data From One Df To Another Issue"