Ich möchte Pandas einen kumulativen Prozentsatz für einen Multi-Index Datenrahmen berechnen und kann einfach nicht bekommen, damit es funktioniert.Kumulierte Prozentsatz für Multi-Index Datenrahmen in Pandas
import pandas as pd
to_df = {'domain': {(12, 12): 2, (14, 14): 1, (15, 15): 2, (15, 17): 2, (17, 17): 1},
'time': {(12, 12): 1, (14, 14): 1, (15, 15): 2, (15, 17): 1, (17, 17): 1},
'weight': {(12, 12): 3,
(14, 14): 4,
(15, 15): 1,
(15, 17): 2,
(17, 17): 5}}
df = pd.DataFrame.from_dict(to_df)
domain time weight
12 12 2 1 3
14 14 1 1 4
15 15 2 2 1
17 2 1 2
17 17 1 1 5
df = df.groupby(['time', 'domain']).apply(
pd.DataFrame.sort_values, 'weight', ascending=True)
cumsum() arbeitet als
df["cum_sum_time_domain"] = df.groupby(['time', 'domain'])['weight'].cumsum()
domain time weight cum_sum_time_domain
time domain
1 1 14 14 1 1 4 4
17 17 1 1 5 9
2 15 17 2 1 2 2
12 12 2 1 3 5
2 2 15 15 2 2 1 1
die Befehle bestimmt läuft selbst
df.groupby(['time', 'domain']).weight.sum()
df.groupby(['time', 'domain'])['weight'].sum()
jedoch plötzlich beide Zuordnungen ergeben funktioniert 'NaNs'
df["sum_time_domain"] = df.groupby(['time', 'domain']).weight.sum()
df
df["sum_time_domain"] = df.groupby(['time', 'domain'])['weight'].sum()
df
Kombinieren der zwei g ives Fehler: ‚auf einem Multi-Index mit mehr als einer Ebene überlappen Verschmelzung ist nicht implementiert‘
df["cum_perc_time_domain"] = 100 * df.groupby(['time', 'domain'])['weight'].cumsum()/df.groupby(
['time', 'domain'])['weight'].sum()
Vielen Dank! Das hat wie ein Zauber funktioniert. – gaussit