J'ai un dataframe
city skills priority acknowledge id_count acknowledge_count
ABC XXX High Yes 11 2
ABC XXX High No 10 3
ABC XXX Med Yes 5 1
ABC YYY Low No 1 5
Je veux regrouper par ville et compétences et obtenir total_id_count à partir de la colonne id_count , divisé en trois colonnes séparées de la priorité comme high.med, low. SIMILAIREMENT pour total_acknowledge_count, acceptez
sortie requise:
total_id_count total_acknowledege_count
city,skills High Med Low Yes No
ABC,XXX 22 5 0 3 3 # 22=11+10 3=(2+1)
ABC,YYY 0 0 1 0 5
J'essaie différentes méthodes comme pivot_table et groupby & stack, mais cela semble très difficile.
Existe-t-il un moyen d'atteindre ce résultat.?
Vous devrez pivoter séparément pour le total_id_count
et l' total_acknowledege_count
ici, car vous avez deux schémas column
/ distincts value
pour l'agrégation:
piv1 = df.pivot_table(index=['city', 'skills'], columns='priority',
values='id_count', aggfunc='sum', fill_value=0)
piv2 = df.pivot_table(index=['city', 'skills'], columns='acknowledge',
values='acknowledge_count', aggfunc='sum', fill_value=0)
piv1.columns = pd.MultiIndex.from_product([['id_count'], piv1.columns])
piv2.columns = pd.MultiIndex.from_product([['acknowledge_count'], piv2.columns])
output = pd.concat([piv1, piv2], axis=1)
print(output)
id_count acknowledge_count
High Low Med No Yes
city skills
ABC XXX 21 0 5 3 3
YYY 0 1 0 5 0
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras