Python和Pandas:将CSV文件中的列转换为整数

时间:2020-09-19 17:17:37

标签: python pandas plot

我的要求是读取带有以下内容的csv文件:日期,最高温度(TMAX),最低温度(TMIN) 稍后,我需要按季度进行总结并绘制条形图。

我正在使用以下代码,但无法正常工作。

#importing necessary libraries
import pandas as pd
import 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()

第一行本身给出了一个错误:TypeError:float()参数必须是字符串或数字,而不是'Period'

如何将这些季度转换为字符串并在y轴上绘制平均温度?

数据

   STATION       DATE  PRCP  TMAX  TMIN np_quarter
0   USW00012921 2010-12-01   0.0  65.0  29.0     2010Q4
1   USW00012921 2010-12-02   0.0  71.0  37.0     2010Q4
2   USW00012921 2010-12-03   0.0  76.0  44.0     2010Q4
3   USW00012921 2010-12-04   0.0  79.0  55.0     2010Q4
4   USW00012921 2010-12-05   0.0  60.0  41.0     2010Q4
5   USW00012921 2010-12-06   0.0  59.0  36.0     2010Q4
6   USW00012921 2010-12-07   0.0  60.0  36.0     2010Q4
7   USW00012921 2010-12-08   0.0  60.0  37.0     2010Q4
8   USW00012921 2010-12-09   0.0  65.0  31.0     2010Q4
9   USW00012921 2010-12-10   0.0  74.0  39.0     2010Q4
10  USW00012921 2010-12-11   0.0  75.0  43.0     2010Q4
11  USW00012921 2010-12-12   0.0  60.0  34.0     2010Q4
12  USW00012921 2010-12-13   0.0  60.0  29.0     2010Q4
13  USW00012921 2010-12-14   0.0  72.0  38.0     2010Q4
14  USW00012921 2010-12-15   0.0  76.0  46.0     2010Q4
15  USW00012921 2010-12-16   0.0  64.0  48.0     2010Q4
16  USW00012921 2010-12-17   0.0  57.0  41.0     2010Q4
17  USW00012921 2010-12-18   0.0  58.0  34.0     2010Q4
18  USW00012921 2010-12-19   0.0  67.0  34.0     2010Q4
19  USW00012921 2010-12-20   0.0  76.0  48.0     2010Q4

1 个答案:

答案 0 :(得分:0)

我认为加载文件存在问题,因为文件用空格而不是逗号分隔。我不确定您要绘制什么,但是这里是每个季度的平均值(最小值和最大值)。我还按照PEP8的建议在运算符周围放置空格,以提高可读性。让我知道它是否无效。

#Reading the file using pandas
df = pd.read_csv(r"C:\Users\home\Download\2285297.csv", delim_whitespace=True)  # delimiter is whitespace not comma
df['DATE'] = pd.to_datetime(df['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([str(avg_tmax.index[0]) + " MAX",
          str(avg_tmax.index[0]) + " MIN"],
         [avg_tmax[0], avg_tmin[0]],
         'o')
plt.ylabel('avg_prec')  # change to str
plt.show()