我需要帮助修改代码的一部分。我刚才已经问过这个问题了。Here is the link。然而,现在我需要找出平均数次。
原始dataframe的示例如下所示:
code scale year week a b c
1111 -5 2017 15 68 68 19
1111 -4 2017 16 30 95 24
1111 -3 2017 17 21 15 94
1111 -2 2017 18 67 30 16
1111 -1 2017 19 10 13 13
1111 0 2017 20 26 22 18
1111 1 2017 21 NaN NaN NaN
1111 2 2017 22 NaN NaN NaN
1111 3 2017 23 NaN NaN NaN
1111 4 2017 24 NaN NaN NaN
1111 5 2017 25 NaN NaN NaN
1111 6 2017 26 NaN NaN NaN
2222 -5 2017 15 13 19 21
2222 -4 2017 16 24 95 23
2222 -3 2017 17 22 32 76
2222 -2 2017 18 21 30 12
2222 -1 2017 19 15 55 17
2222 0 2017 20 23 22 23
2222 1 2017 21 NaN NaN NaN
2222 2 2017 22 NaN NaN NaN
2222 3 2017 23 NaN NaN NaN
2222 4 2017 24 NaN NaN NaN
2222 5 2017 25 NaN NaN NaN
2222 6 2017 26 NaN NaN NaN
....量表的间隔可能不同,但我事先就知道了。计算应该从刻度= 0的位置开始。在每个周期从1到6的例子中,我需要使用前面的值在-5到0的范围内计算平均值。和以前一样,使用新的计算值。计算的实质是计算从code列到scale中的每个唯一位置的平均值,从-5到6。虽然对于列code中的所有值的间隔是相同的,但是唯一值的数目可能有所不同。所以我想为-5到6之间的每一个间隔写一个循环,但我不能。此外,还有一个问题,即作为a, b, c的列数可能有所不同。例如,我需要为从-5到6的每一个间隔申请这段代码。
import numpy as np
import pandas as pd
#data is your dataframe name
column_list = list(data.columns.values)[4:]
for column_name in column_list :
column = data[column_name].values
#converted pandas series to numpy series
for index in xrange(0,column.shape[0]):
#iterating over entries in the column
if np.isnan(column[index]):
column[index] = np.nanmean(column.take(range(index-5,index+1),mode='wrap'))结果应该如下所示:
code scale year week a b c
1111 -5 2017 15 68 68 19
1111 -4 2017 16 30 95 24
1111 -3 2017 17 21 15 94
1111 -2 2017 18 67 30 16
1111 -1 2017 19 10 13 13
1111 0 2017 20 26 22 18
1111 1 2017 21 37 41 31
1111 2 2017 22 32 36 33
1111 3 2017 23 32 26 34
1111 4 2017 24 34 28 24
1111 5 2017 25 28 28 25
1111 6 2017 26 32 30 27
2222 -5 2017 15 13 19 21
2222 -4 2017 16 24 95 23
2222 -3 2017 17 22 32 76
2222 -2 2017 18 21 30 12
2222 -1 2017 19 15 55 17
2222 0 2017 20 23 22 23
2222 1 2017 21 20 42 29
2222 2 2017 22 21 46 30
2222 3 2017 23 20 38 31
2222 4 2017 24 20 39 24
2222 5 2017 25 20 40 26
2222 6 2017 26 21 38 27
...我将非常感谢任何帮助!
UPD列code包含各个设备代码。列a,b,c显示每个区域在某一周和一年内的设备数量。其任务是使用已知值的平均值来预测每个区域中每个代码的设备数量,以确定未来的日期。为了便于计算,需要标度。例如,以代码1111为例。取a列。已知范围内的scale上的值(-5,0)。对于它们来说,计算规模为1的单元格的平均值。这将是37。对于下一个单元格,取标度为(-4,1)的值。在它中,值将等于31.833。等等,所有的细胞,其中scale从1到6。对于列a, b, c也是如此。我们采用以下代码2222。我们也为他做同样的事。以列a为例。对于规模为1的单元格,计算已知值的平均值( scale又是从-5到0,但code是2222)。我们得到了19.66。诸若此类。每个唯一代码的行数是相同的(从-5到6)。但可能有很多密码。
我希望我能更好地解释这个问题。
发布于 2017-08-29 18:41:06
我们需要的是一种获得移动平均值的方法--我可能错了,但我不认为在熊猫中有一个处理这个问题的功能(考虑到熊猫确实实现了ewa()和rolling_mean(),我想这可能也不奇怪)。在这里使用递归是有意义的,因为它不太深。
def moving_average(data, window, periods_forward):
"""docs"""
try:
data.shape[1]
except IndexError:
import sys
print("Data shape %s found. If there is only one sample please reshape the data using .reshape(-1, 1)." % data.shape)
sys.exit()
# Base case: Kill the recursion once we've created enough forward looks.
if periods_forward == 0:
return data
else:
data = np.concatenate([data, data[-window:, :].mean(axis=0).reshape(1,-1)])
periods_forward -= 1
return moving_average(data, window, periods_forward)
# Reset values in the dataframe.
columns = ['a', 'b', 'c']
for code in df.code.unique():
df.loc[df.code == code, columns] = moving_average(
df.loc[df.code == code, columns].dropna().values, window=6, periods_forward=6)发布于 2017-08-30 05:01:53
假设您的数据与所提供的示例相同,您可以这样做。
colSelector = df.columns.values[4:]
for index,row in df.iterrows():
if np.isnan(row[4:].values).any():
col = colSelector[np.isnan(row[4:].values)]
df.loc[index,col] = np.round(df.loc[index-6:index,col].mean(),0)我假设您可能拥有比a、b和c更多的平均列,但无论哪种方式,这都是可行的。另外,我们可以做一些布尔索引来查找NaN值并选择对它们进行平均值,而不是遍历每一列,从而消除了第一个循环。
注意:如果它只是a-c列,并且在那些列之后确实有数据不是平均值,那么将所有的[4:]更改为[4:7]。
https://stackoverflow.com/questions/45943258
复制相似问题