我有一个日期范围为2018年1月12日至8月3日的数据集,其中包含一些值:
df_luminosidad
数据帧的维数为:
df_luminosidad.shape
(9752, 2)
每行数据的频率为半小时。第一行从2018-01-12
开始
df_luminosidad.iloc[0]
Fecha: 2018-01-12 00:17:28
Luz (lux) 1
Name: 0, dtype: object
最后一行结束于2018-08-03
df_luminosidad.tail(1)
Fecha: Luz (lux)
9751 2018-08-03 23:44:59 1
有许多行对应于一天,如上面所示。你知道吗
我想创建一个新的dataframe,只选择1天作为Fecha:
列的值(没有重复的日期),并从Luz(lux) column
所选日期的所有现有值中选择平均值
这看起来像这样:
| Fecha: | Luz(lux) - Average each day values |
| 2018-01-12 | 9183.479167 |
| 2018-01-13 | 7431.8125 |
| 2018-01-14 | 11073.1875 |
| 2018-01-15 | 11456.25 |
| . . |
| . . |
| . . |
| 2018-08-03 | 11331.229167 |
我正在使用pd.date_range()
函数来创建一个数据帧,其中的值从df_luminosidad
前一个数据帧到指定的频率,如@piRSquaredin this answer
从1月12日到8月03日有203天(28周),然后我选择204 like period
属性,我使用freq
属性日历日频率D
df = pd.DataFrame(dict(
Date=pd.date_range('2018-01-12', periods=204, freq='D'),
Value=df_luminosidad['Luz (lux)'].mean()
))
在这里之前,这个方法是好的,但是我对如何计算用pd.date_range
选择的每一天的Luz (lux)
值的平均值有疑问,因为此时我只得到从2018-01-12
到2018-08-03
的所有天的平均值,如下所示:
| Date | Value |
| 2018-01-12 | 11228.888331 |
| 2018-01-13 | 11228.888331 |
| 2018-01-14 | 11228.888331 |
| 2018-01-15 | 11228.888331 |
| . . |
| . . |
| . . |
| 2018-08-03 | 11331.229167 |
我已经通过每个Fecha:
列的值生成了一个数据帧,我可以分别得到它们的平均值,但是这迫使我去阅读
每个文件日。你知道吗
如何以循环的方式将一天的所有值分组为一个平均值,并将它们放在一个数据帧中?你知道吗
对于时间序列分析,如果可能,首先将日期列转换为索引,然后再方便地与日期一起使用。你知道吗
然后可以将任何日期值提取到另一个临时数据帧中。你知道吗
现在使用for循环,从“dates”列表中对每个日期进行rander,然后将数据集所需的部分选择到另一个数据帧中。你知道吗
这可能是一个天真的方法,但现在我有这么多的建议给你。希望有用。你知道吗
我认为需要^{} 和} :
mean
或聚合mean
和^{或:
另一种解决方案是
DatetimeIndex
:相关问题 更多 >
编程相关推荐