繁体   English   中英

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

[英]Python and pandas: Convert a column in CSV file to integer

我的要求是读取一个带有列的 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() 参数必须是字符串或数字,而不是“期间”

如何将这些季度转换为字符串并在 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

我认为加载文件存在问题,因为它用空格而不是逗号分隔。 我不确定你想绘制什么,但这里是每个季度的平均值(最小值和最大值)。 我还在 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM