gpt4 book ai didi

python-2.7 - 在python中按时间分组和绘制数据

转载 作者:行者123 更新时间:2023-12-03 11:22:14 25 4
gpt4 key购买 nike

我有一个 csv 文件,我正在尝试绘制每月某些值的平均值。我的 csv 文件的结构如下所示,所以我认为我应该每天对我的数据进行分组,然后是每月分组以计算平均值。

timestamp,heure,lat,lon,impact,type
2007-01-01 00:00:00,13:58:43,33.837,-9.205,10.3,1
2007-01-02 00:00:00,00:07:28,34.5293,-10.2384,17.7,1
2007-01-02 00:00:00,23:01:03,35.0617,-1.435,-17.1,2
2007-01-03 00:00:00,01:14:29,36.5685,0.9043,36.8,1
2007-01-03 00:00:00,05:03:51,34.1919,-12.5061,-48.9,1

我正在使用此代码:
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

df= pd.read_csv("ave.txt", sep=',', names =["timestamp","heure","lat","lon","impact","type"])
daily = df.set_index('timestamp').groupby(pd.TimeGrouper(key='timestamp', freq='D', axis=1), axis=1)['impact'].count()
monthly = daily.groupby(pd.TimeGrouper(freq='M')).mean()
ax = monthly.plot(kind='bar')
plt.show()

但是,我不断收到这样的错误:

KeyError: 'The grouper name timestamp is not found'



有任何想法吗 ??

最佳答案

您收到此错误是因为您设置了 timestamp列到 index .尝试删除 key='timestamp'来自 TimeGrouper()set_index方法,它应该按您的预期分组:

daily = df.set_index('timestamp').groupby(pd.TimeGrouper(freq='D', axis=1), axis=1)['impact'].count()

或者
daily = df.groupby(pd.TimeGrouper(key='timestamp', freq='D', axis=1), axis=1)['impact'].count()

关于python-2.7 - 在python中按时间分组和绘制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44306730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com