简体   繁体   中英

Create a bar graph with a DataFrame

I have a data frame that looks like this:

     Cursos   Per1   Per2   Per3   Per4  ...  Per46  Per47  Per48  Per49  Per50
0    Curso1   True  False  False  False  ...  False  False  False   True  False
1    Curso2  False  False  False  False  ...  False  False  False  False  False

Where the columns are gropued by 10 - ('Per1' to 'Per10') ('Per11' to 'Per20')... And each of these groups represents a day of the week - Monday, Tuesday etc.

I have to create a bar graphic with the values of every of these groups of 'days' where the value is 'True' (That means, there is class in that period in that day).

In the end, there should be 5 bars (one per day) with the infomation regarding the amounts of "Per" that have values of 'True'.

This proyect is already 4,746 lines long, and this is the final step, I would greatly appreciate any help!

Example of the graphic:

在此处输入图像描述

Where the days are at the bottom (they are in spanish in this case, and the number of periods with 'true' values are at the left.

You can use groupby() along the columns to sum:

(df.set_index('Cursos')
   .groupby(np.arange(50)//10, axis=1)
   .sum().T.plot.bar()
)

Try:

df.set_index('Cursos').groupby(np.arange(50)//10, axis=1)\
  .sum().sum()\
  .rename(index={n:i for n,i in enumerate(['lunes', 'martes', 'miercoles', 'jueve', 'viernes'])})\
  .plot.bar()

Output:

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM