Using count from db for matplotlib bar graph using pandas?

Gadawg099 :

I've been trying to learn some data analysis, and I've been trying to write something to look at the counts of different things.

So the db has information about accidents in the work place, basically everything that would be filled out when someone gets hurt, to analyze and see when and where accidents are happening.

Like, which shift is having the most, which department, what time of day are most of the accidents happening, stuff like that.

I've been messing around and messing around with it and can't seem to get it to work the way id like.

Right now, I'm just trying to get it to work for shifts, I feel as if I could get it to work for shifts I could probably get it to work for everything else.

So, I need it to show how many accidents happened on each shift.

But when I try to make the graph, its only showing them even across the board. It looks like its showing the total count of all accidents under each shift. In my DB I have 29 different accidents.

So its like the graph is showing

1A - 29 2A - 29 1B - 29

When it should be something like

1A - 13 2A - 8 1B - 8

Ive been trying for hours and hours to figure this out and can't seem to figure it out.

I only used the

counter = df.groupby(['shift']) ['shift'].count() print(counter)

part to see if it was giving me the information that I wanted, which it did. I just can't get it to do right when I make it into a graph.

import pandas as pd
import sqlite3
import numpy as np
import matplotlib.pyplot as plt

conn = sqlite3.connect('accident.db')
c = conn.cursor()
df = pd.read_sql_query("Select * from accidents", conn)

df['count'] = 1

counter = df.groupby(['shift']) ['shift'].count()
print(counter)

plt.bar(df['shift'], df['count'].count())
plt.show()

c.execute("""CREATE TABLE IF NOT EXISTS accidents(
            reference real,
            first text,
            last text,
            date real,
            time real,
            department text,
            class text,
            type text,
            shift blob
            )""")

#

conn.commit()
conn.close()
Vorsprung durch Technik :

This should work:

df.groupby(['shift']).count()['count'].plot(kind='bar')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170504&siteId=1