I want to be able to make a bar chart with the number of occurrences of a certain room (stored in a csv file). the number of rooms is not defined in the beginning.
this is the data stored in the csv file:
this is the type of graph i want to show, but with the code i currently have it doesn't show the number of occurrences.
what can i do to solve this problem?
this is my code so far.
with open('calls.csv') as File:
plots = csv.reader(File, delimiter = ',')
for row in plots:
if (row[0] not in x):
x.append(row[0])
numberOfRooms = len(x)
for i in range(numberOfRooms):
occurence = 0
for row in plots:
if(x[i] == row[0]):
occurence = 1
y.append(occurence)
plt.bar(x,y, width = 0.7, label="Number of calls")
plt.xlabel('room')
plt.ylabel('number of calls')
plt.title('Number of calls per room')
plt.legend()
plt.show()
CodePudding user response:
Since you tagged Pandas, you can do:
# read dataframe from csv
df = pd.read_csv('calls.csv')
# count the values in the first column
counts = df.iloc[:,0].value_counts()
# plot the count
counts.plot.bar()
That said, you can certainly use csv
package, but you should probably use a different data structure, e.g. dictionary:
with open('calls.csv') as File:
data = csv.reader(File, delimiter = ',')
counts = {}
# loop through data
for row in data:
room = row[0]
# initiate if room is first seen
if room not in counts: counts[room] = 0
# increase the count
counts[room] = 1
# plot the counts
plt.bar(list(counts.keys()), list(counts.values()) )
CodePudding user response:
For a task like this, using only the "built-ins", I'd use a dictionary to accumulate the data
data = {}
with open('calls.csv') as File:
plots = csv.reader(File, delimiter = ',')
for row in plots:
# increments the counter of occurrences,
# if the key is not already in there, .get() will return the 0 as a default
data[row[0]] = data.get(row[0],0) 1
# at this point we have processed the whole file, so let's prepare data for plotting
# we take the keys of the dictionary and sort them for the x axis
x = list(data.keys())
x.sort()
# the we take the values, the count of occurrences, in the same order
y = [data[i] for i in x]
# then just ... plot it.
plt.bar(x, y, width = 0.7, label="Number of calls")
plt.xlabel('room')
plt.ylabel('number of calls')
plt.title('Number of calls per room')
plt.legend()
plt.show()
CodePudding user response:
Without pandas
, you can save the number of calls per rooms in a dict
and use this to plot:
import csv
import matplotlib.pyplot as plt
rooms = dict()
with open("calls.csv") as infile:
csvreader = csv.reader(infile)
for row in csvreader:
if row[0] not in rooms:
rooms[row[0]] = 0
rooms[row[0]] = 1
plt.bar(rooms.keys(), rooms.values(), width=0.7)
plt.xlabel("room")
plt.ylabel("number of calls")
plt.title("Number of calls per room")