为什么我的其中一个列名称中有很多空格?

问题描述 投票:0回答:1

为什么列名TAVG下面有一堆空格?

image of table

我创建了这个数据框,列名的格式很奇怪。我的列名是“DATE”和“TAVG”还是“DATE”和“TAVG

”?我在引用这些列时遇到问题,并且代码无法找到它们。

编辑:我明白了,这是因为“DATE”列实际上是一个索引。一旦我将其移至一列,它们就会排成一列,我就可以使用“日期”作为一列。

python pandas
1个回答
0
投票

我想通了,这是因为“DATE”列实际上是一个索引。一旦我将其移至一列,它们就会排成一列,我就可以使用“日期”作为一列。

#Average temp by month
weather_data_monthly_avg = weather_data.groupby(pd.PeriodIndex(weather_data['DATE'], freq="M"))['TAVG'].mean()
#Put output into dataframe
df_weather_monthly_avg = pd.DataFrame(weather_data_monthly_avg)
#Move DATE from index to column
df_weather_monthly_avg = df_weather_monthly_avg.reset_index()
df_weather_monthly_avg = df_weather_monthly_avg[['DATE','TAVG']]
df_weather_monthly_avg

    DATE    TAVG
0   2021-01 46.096774
1   2021-02 47.535714
2   2021-03 59.129032
3   2021-04 62.733333
4   2021-05 69.677419
5   2021-06 76.666667
6   2021-07 78.774194
7   2021-08 79.258065
8   2021-09 73.466667
9   2021-10 66.322581
10  2021-11 52.366667
11  2021-12 55.387097
© www.soinside.com 2019 - 2024. All rights reserved.