Wie kann ich die Reihenfolge des Index basierend auf meiner Präferenz in Multi-Index Pandas DataFrames sortieren?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie kann ich die Reihenfolge des Index basierend auf meiner Präferenz in Multi-Index Pandas DataFrames sortieren?

by Anonymous » 11 Apr 2025, 20:33

Ich habe einen Pandas DataFrame df . Es hat Multi-Index mit GX.region und Szenario_Model. Wenn ich es zeichne, kommt es in der gleichen Reihenfolge. Ich möchte es jedoch als PES, TES und DES ordnen und es entsprechend darstellen. Ist es möglich, dies in Python Pandas DataFrame zu erreichen? < /P>

Code: Select all

    dict = {('Value', 2023, 'BatteryStorage'): {('Central Africa', 'des'): 0.0,
('Central Africa', 'pes'): 0.0,
('Central Africa', 'tes'): 0.0,
('Eastern Africa', 'des'): 0.0,
('Eastern Africa', 'pes'): 0.0,
('Eastern Africa', 'tes'): 0.0,
('North Africa', 'des'): 0.0,
('North Africa', 'pes'): 0.0,
('North Africa', 'tes'): 0.0,
('Southern Africa', 'des'): 504.0,
('Southern Africa', 'pes'): 100.0,
('Southern Africa', 'tes'): 360.0,
('West Africa', 'des'): 0.0,
('West Africa', 'pes'): 0.0,
('West Africa', 'tes'): 0.0},
('Value', 2023, 'Biomass PP'): {('Central Africa', 'des'): 0.0,
('Central Africa', 'pes'): 0.0,
('Central Africa', 'tes'): 0.0,
('Eastern Africa', 'des'): 40,
('Eastern Africa', 'pes'): 10,
('Eastern Africa', 'tes'): 50,
('North Africa', 'des'): 0.0,
('North Africa', 'pes'): 0.0,
('North Africa', 'tes'): 0.0,
('Southern Africa', 'des'): 90.0,
('Southern Africa', 'pes'): 43.0,
('Southern Africa', 'tes'): 50.0,
('West Africa', 'des'): 200.0,
('West Africa', 'pes'): 150.0,
('West Africa', 'tes'): 100}}

df_sample = pd.DataFrame.from_dict(dict)
df_sample.plot(kind = "bar",
stacked = True)

Top