提取属于一天的数据值并计算每天的平均值

2024-10-01 07:19:10 发布

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

我有一个日期范围为2018年1月12日至8月3日的数据集,其中包含一些值:

enter image description here

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-122018-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:列的值生成了一个数据帧,我可以分别得到它们的平均值,但是这迫使我去阅读 每个文件日。你知道吗

如何以循环的方式将一天的所有值分组为一个平均值,并将它们放在一个数据帧中?你知道吗


Tags: 数据dfdate属性valuerange频率平均值
2条回答

对于时间序列分析,如果可能,首先将日期列转换为索引,然后再方便地与日期一起使用。你知道吗

df = pd.read_csv('file_name.csv', parse_dates =['Fecha'], index_col='Fecha')

然后可以将任何日期值提取到另一个临时数据帧中。你知道吗

dates = pd.date_range(start='2018-01-12 ', end='2018-08-03 ')

现在使用for循环,从“dates”列表中对每个日期进行rander,然后将数据集所需的部分选择到另一个数据帧中。你知道吗

new_df = pd.DataFrame()   # Creating temporary data frame to store each day value
for temp_date in dates:
    required_date = str(temp_date)[:10]     # this is to fetch only date value from whole time stamp
    new_df = df1[required_date]  
# Now our requrired data is in new dataframe, and we can do all things to our new dataframe. 

这可能是一个天真的方法,但现在我有这么多的建议给你。希望有用。你知道吗

我认为需要^{}mean或聚合mean^{}

df_luminosidad['Fecha:'] = pd.to_datetime(df_luminosidad['Fecha:'])

df = df_luminosidad.resample('D', on='Fecha:')['Luz (lux)'].mean().reset_index()

或:

df = (df_luminosidad.groupby(pd.Grouper(key='Fecha:', freq='D'))['Luz (lux)']
                    .mean()
                    .reset_index())

另一种解决方案是DatetimeIndex

df_luminosidad['Fecha:'] = pd.to_datetime(df_luminosidad['Fecha:'])
df_luminosidad = df_luminosidad.set_index('Fecha:')


df = df_luminosidad.resample('D')['Luz (lux)'].mean().reset_index()
df = df_luminosidad.groupby(pd.Grouper(freq='D'))['Luz (lux)'].mean().reset_index()

相关问题 更多 >