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

2024-09-27 19:10:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我的要求是读取包含以下列的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

Tags: csvimportdfdatebynp绘制plt
1条回答
网友
1楼 · 发布于 2024-09-27 19:10:03

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

相关问题 更多 >

    热门问题