[Tutor] Python and pandas: Convert a column in CSV file to integer
Bvssrs Guntur
bvssrsv at gmail.com
Sun Sep 20 12:44:00 EDT 2020
My requirement is to read a csv file with columns: Date, Maximum
temperature (TMAX), Min temperature(TMIN) Later I need to summarize by
quarter and plot a bar graph.
I am using the below code but it is not working.
#importing necessary librariesimport pandas as pdimport matplotlib.pyplot as plt
#Reading the file using pandas
df = pd.read_csv(r"C:\Users\home\Downloads\2285297.csv", parse_dates = ['DATE'])
# Reading each column
np_maxtemp= df.loc[:,['TMAX']]
np_mintemp= df.loc[:,['TMIN']]
np_date= df.loc[:,['DATE']]
#Summarizing Max temp by each quarter
df['np_quarter'] = pd.PeriodIndex(df.DATE, freq='Q')#It gives me list
of all quarters 0 2010Q1 1 2010Q2
avg_tmax=df.groupby(by=['np_quarter'])['TMAX'].mean()
avg_tmin=df.groupby(by=['np_quarter'])['TMIN'].mean()#It gives me
averages by quarter
# Then I want to plot with quarters on x-axis and temperature bar
plots on y-axis
plt.plot(df['np_quarter'])
plt.ylabel(avg_prec)
plt.show()
First line itself is giving an error: TypeError: float() argument must
be a string or a number, not 'Period'
How can I convert those quarters to string and plot average
temperatures on y-axis?
Thanks,
bvssrs!
More information about the Tutor
mailing list