python - 数据框的日平均值
问题描述
目前我正在尝试编写一个用于预测分析的程序,但我在弄清楚如何在数据帧上打印出每日中值时遇到了一些问题我在尝试找出“正常运行时间”的每日中值时遇到了问题一个数据框。
我的代码如下所示:
import glob
import os
import pandas as pd
import numpy as np
import time
import matplotlib.pyplot as plt
path = r'C:\Users\eneki\OneDrive\001. HHS\.LVNL\Documentatie\MNT-COM\testdata'
filenames = glob.glob(os.path.join(path + '/*.csv'))
li= []
for filename in filenames:
df = pd.read_csv(filename, index_col=None, header= 0)
li.append(df)
def daily_mean(df, date, col):
return df[date][col].mean()
data=np.random.rand(10)
columns = Data['up-time']
times= pd.date_range('14/10/2021', freq='1D', periods = 10)
Data = pd.DataFrame(data=data, index=times, columns=columns)
dates= df.index.strftime('%d%m%Y').unique()
means=df.groupby(pd.Grouper(freq='1D')).mean()
这是我的数据框的样子:
In [152]: Data.iloc[:, : 8]
Out[152]:
Date Time ... OS Version up-time
0 14/10/2021 00:05:18 ... 7.0.2 17867351 U2 2900565
1 14/10/2021 00:10:19 ... 7.0.2 17867351 U2 2893095
2 14/10/2021 00:20:21 ... 7.0.2 17867351 U2 2901468
3 14/10/2021 00:25:19 ... 7.0.2 17867351 U2 2893995
4 14/10/2021 00:35:18 ... 7.0.2 17867351 U2 2902365
... ... ... ... ...
2414 26/10/2021 11:10:18 ... 7.0.2 17867351 U2 182031
2415 26/10/2021 11:20:17 ... 7.0.2 17867351 U2 182631
2416 26/10/2021 11:25:18 ... 7.0.2 17867351 U2 182931
2417 26/10/2021 11:35:20 ... 7.0.2 17867351 U2 183534
2418 26/10/2021 11:40:20 ... 7.0.2 17867351 U2 183833
[2419 rows x 8 columns]
我得到的错误是:
ValueError: Shape of passed values is (10, 1), indices imply (10, 2419)
当我应用确切的数字时,我得到另一个错误:
AttributeError: 'RangeIndex' object has no attribute 'strftime'
是不是因为日期/时间的标准格式,无论如何,我真的不知道如何解决它。有谁熟悉这个问题?
解决方案
以下应该工作。
means = df.groupby("Date")["up-time"].median()
您的代码中的第一个错误来自:
columns = Data['up-time']
它有 2419 个值的列表。然后它尝试根据这些值命名列,但只有 1 列,从错误代码中可见。
ValueError: Shape of passed values is (10, 1), indices imply (10, 2419)
我希望这是有道理的。:)
推荐阅读
- mysql - 最小、最大 SQL 查询优化
- javascript - 如何在 v-for 中配置前 3 个值
- webpack - 如何停止显示来自 webpack 的构建日志?
- android - Firebase UI 依赖项中的 Gradle 构建错误
- android - 收发数据短信绝对不行
- java - java拼图扩展jar库
- git - 如何清除超过大小限制的 Bitbucket 存储库中的空间?
- python - 在 python 上创建“开始按钮”Facebook Messenger
- algorithm - 安全断路算法解法
- ruby-on-rails - 如何使用带有查询参数的 Rails 片段缓存自动到期?