I am wanting to create a multiple column plot, with the values of a variable each month for four years, grouped by year. So, each year is one point on the X-axis, with 12 columns.
To do this I've created the dataframe, df_all
, which contains the values:
df_all = pd.DataFrame({"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"2016": bars_16["value"],
"2017": bars_17["value"],
"2018": bars_18["value"],
"2019": bars_19["value"]})
df_all = df_all.set_index("month")
df_all.head()
It looks like:
2016 2017 2018 2019
month
Jan 0 1016340 1815983 3163752
Feb 0 871166 1839012 2967114
Mar 0 910442 1943507 2827649
Apr 0 926362 1870525 2681053
May 97162 1061573 1753449 2834637
I want to slice every month's value out into a series of four numbers, like getting a jan
variable equal to [0, 1016340, 1815983, 3163752]
I tried the following:
jan = df_all.loc["Jan", :]
feb = df_all.loc["Feb", :]
mar = df_all.loc["Mar", :]
apr = df_all.loc["Apr", :]
may = df_all.loc["May", :]
jun = df_all.loc["Jun", :]
jul = df_all.loc["Jul", :]
aug = df_all.loc["Aug", :]
sep = df_all.loc["Sep", :]
octo = df_all.loc["Oct", :]
nov = df_all.loc["Nov", :]
dec = df_all.loc["Dec", :]
But what I got is this (using jan
as an example):
2016 0
2017 1016340
2018 1815983
2019 3163752
It seems that I'm getting the top index of the years. How should I just get the values in the rows themselves? Or is there a way to remove the [2016, 2017, 2018, 2019]
from jan
through dec
?
Thanks for the help.
You could try this:
rows = {month: row.to_list() for month, row in df.iterrows()}
jan = rows["jan"] # [0, 1016340, 1815983, 3163752]
feb = rows["feb"] # [0, 871166, 1839012, 2967114]
mar = rows["mar"] # [0, 910442, 1943507, 2827649]
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.