How do I fix this type error ('value' must be an instance of str or bytes, not a float) on Python

Viewed 32092

I want to plot a graph for Covid-19 in India and so far there's no problem when I manually input my data as x and y axis. But since the data is quite long and when I want to read it as .csv file, it gives me this error 'value' must be an instance of str or bytes, not a float. I have also try to wrap int(corona_case), but giving me another new error, cannot convert the series to <class 'int'. Also I would be very appreciate if someone can suggest me tutorials on plotting graph involving datetime using python since this is my first time learning python. I am using Python 3.

p/s I seem can't find a way to share my csv file so I am gonna leave it in snippet.

import pandas as pd
from datetime import datetime, timedelta
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates
import numpy as np

plt.style.use('seaborn')

data = pd.read_csv('india.csv')
corona_date = data['Date']
corona_case = data['Case']
 
plt.plot_date (corona_date, corona_case, linestyle='solid')

plt.gcf().autofmt_xdate()

plt.title('COVID-19 in India')
plt.xlabel('Date')
plt.ylabel('Cumulative Case')

plt.tight_layout()

plt.show()

Date,Case
2020-09-30,6225763
2020-10-01,6312584
2020-10-02,6394068
2020-10-03,6473544
2020-10-04,6549373
2020-10-05,6623815
2020-10-06,6685082

3 Answers

convert all DataFrame columns to the int64 dtype

df = df.astype(int)

convert column "a" to int64 dtype and "b" to complex type

`df = df.astype({"a": int, "b": complex})` 

convert Series to float16 type

s = s.astype(np.float16) 

convert Series to Python strings

s = s.astype(str)

convert Series to categorical type - see docs for more details

s = s.astype('category')

You can convert the type of column in pandas dataframe like this.

corona_case = data['case'].astype(int) # or str for string

If that is what you are trying to do.

I couldn't give further information but this might work:

corona_date = (data['Date']).astype(str)
corona_case = (data['Case']).astype(str)
Related