When I run the following code,I get this error:ValueError: invalid literal for int() with base 10: “(1, 0, 'Friday')”


When I run the following code,I get this error:ValueError: invalid literal for int() with base 10: “(1, 0, 'Friday')”
When I run the following code,I get this
ValueError: invalid literal for int() with base 10: "(1, 0, 'Friday')"
pointing to the line:
monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0]))
Output: Plot I need that compares monthly readership among subscribers and customers
import calendar
import datetime
infile_csv = 'C:/pythonscripts/NYC-2016-Summary.csv'
def read_from_csv(input_csvfile, duration=False, month=False, hour=False, day_of_week=False):
# assign columns name
if duration==True:
col_name='duration'
elif month==True:
col_name='month'
elif hour==True:
col_name='hour'
elif day_of_week==True:
col_name='day_of_week'
# init lists for output
n_ridership4column =
n_ridership_sub =
n_ridership_cust =
with open(infile_csv, 'r') as f_in:
filereader = csv.DictReader(f_in)
for row in filereader:
n_ridership4column.append(row[col_name])
if row['user_type'] == 'Subscriber':
n_ridership_sub.append(row[col_name])
else:
n_ridership_cust.append(row[col_name])
return n_ridership4column, n_ridership_sub, n_ridership_cust
# using the function above to get monthly ridership
monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0]))
monthwise_sub = list(map(int, read_from_csv(infile_csv, month=True)[1]))
monthwise_cust = list(map(int, read_from_csv(infile_csv, month=True)[2]))
The below code is for plotting. This is not required for the question but for the clarity of output.
fig, ax = plt.subplots()
bins = [i for i in range(1,14)]
#upper bound is 14 to accomodate bin for december
#### Plotting monthly total along with customers and subscribers stacked
ax.hist(monthwise, bins=bins, edgecolor='k', align='left', label='Total Ridership', stacked= True)
ax.hist(monthwise_sub, bins=bins, edgecolor='k', align='left', label='Subscribers', stacked=True)
ax.hist(monthwise_cust, bins=bins, edgecolor='k', align='left', label='Customer', stacked=True)
ax.set_xticks(bins[:-1])
ax.set_xticklabels(list(calendar.month_abbr[i] for i in bins[:-1]))
plt.title('Monthly Ridership in NYC', fontsize=16)
plt.xlabel('Monthly', fontsize=14)
plt.ylabel('Rides', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.legend()
plt.show()
This is my sample .csv file
duration month hour day_of_week user_type
13.98333333 (1, 0, 'Friday') (1, 0, 'Friday') Customer
11.43333333 (1, 0, 'Friday') (1, 0, 'Friday') Subscriber
5.25 (1, 0, 'Friday') (1, 0, 'Friday') Subscriber
12.31666667 (1, 0, 'Friday') (1, 0, 'Friday') Subscriber
20.88333333 (1, 0, 'Friday') (1, 0, 'Friday') Customer
8.75 (1, 0, 'Friday') (1, 0, 'Friday') Subscriber
10.98333333 (1, 0, 'Friday') (1, 0, 'Friday') Subscriber
what can be done to rectify?.i tried adding .applystr() .it doesnt work.
– user10019895
2 days ago
Please edit your question to include a sample CSV file that reproduces this error. I ran your code on a CSV file I made up and it completed without error.
– Luke Woodward
12 hours ago
I have added my csv file and also the code used for plotting has been included. I ran it in jupyter notebook . python 3
– user10019895
1 hour ago
I tried to fix the formatting but the CSV sample is too messy. Could you please edit the sample to provide it in a code block which can be usefuly copy/pasted for examination and experiments? Proper code formatting is simple - just paste the code, select it, and type ctrl-K. (In the mobile interface, you need to manually indent each code line with four spaces.) See also Markdown help.
– tripleee
17 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You are trying to convert a tuple to int...
– Rakesh
2 days ago