How do I fix this type error ('value' must be an instance of str or bytes, not a float) on Python
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')
Comments
-
Siti almost 2 years
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
-
Siti over 3 yearsHi, I have seen and tried this one too before, this too, would give me an error, ValueError: invalid literal for int() with base 10: '\t'
-
Hasnat over 3 yearsthat means there are non number characters in the case column in this case a tab('\t'). You can try removing these characters.
pd.to_numeric(data['case'], error='coerce')
which converts number values in string to numbers and non-numeric values to null then remove null valuesdata.dropna(subset=['case'])
. -
Siti over 3 yearsI've tried that, but still giving me a value error. I think there must be something wrong with my column date, as I put non number characters 2020-01-30 ValueError: invalid literal for int() with base 10: '2020-01-30'.
-
Siti over 3 yearsI have also add these 2 lines, but still giving me the same error.
date_format = mpl_dates.DateFormatter('%b, %d, %Y')
plt.gca().xaxis.set_major_formatter(date_format)
-
Hasnat over 3 yearsPandas reads datetime in string format from csv. You can convert it to datetime as
data['Date'] = pd.to_datetime(data['Date'])
and that works with matplotlib. It would be better if you can share the csv.