Using CSV files in Python with pandas

I am having some issues running this for the first time. I am an old excel boomer and my job wants me to learn python. While I have uploaded my csv, I cant get replit to read it. I am getting other errors in Jupyter so this is code check as well.

# Calls by the hour in a bar graph

import pandas as pd

import matplotlib.pyplot as plt

# read in call center data and set low_memory option to false

data = pd.read_csv('C:\\Python\\all2022.csv', low_memory=False)

# convert the timestamp column to datetime type

data['time'] = pd.to_datetime(data['time'])

# extract hour and day of the week from timestamp column

data['hour'] = data['time'].dt.hour

data['day_of_week'] = data['time'].dt.weekday.map({0:'Monday',1:'Tuesday',2:'Wednesday',3:'Thursday',4:'Friday',5:'Saturday',6:'Sunday'})

# group data by hour and day of the week and calculate number of calls

calls_per_hour = data.groupby(['hour', 'day_of_week']).size().reset_index(name='counts')

# pivot the data to create a dataframe with hours as columns and days of the week as rows

calls_pivot = calls_per_hour.reset_index().pivot(index='hour', columns='day_of_week', values='counts')

#create figure and axis for the bar chart

fig, ax = plt.subplots(figsize=(12,6))

#sort the dataframe by hour

calls_pivot.sort_index(inplace=True)

#plot the dataframe

calls_pivot.plot(kind='bar', stacked=True, ax=ax)

#format the x-axis

ax.set_xticklabels(["12am", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am", "12pm", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm"])

ax.set_xlabel('Hour')

ax.set_ylabel('Number of calls')

ax.set_title('Calls per Hour by Day of Week')

print(calls_pivot.sum().sum())

plt.show()

thank you for any advice.

Can you give us the error/exception? We cannot help if we don’t know what’s wrong.

1 Like

Try changing the read line from C:\\Python\\all2022.csv to /home/runner/insert-your-repl-name-here/all2022.csv. (Replit uses Linux, not Windows)

3 Likes