Код: Выделить всё
x.field.rolling(window=5,min_periods=1).mean()
Я пробовал с помощью решение, предложенное на этой странице. Итак, я сделал следующее:
Код: Выделить всё
x.field.apply(lambda x: x.rolling(window=5,min_periods=1).mean())
Код: Выделить всё
+---------+---------+-------+--------------------+
| machin | machin | truc | a column of series |
+---------+---------+-------+--------------------+
| machin1 | machin1 | truc1 | 1 |
| | | truc2 | 2 |
| | | truc3 | 3 |
| | | truc4 | 4 |
| machin2 | machin2 | truc1 | 100 |
| | | truc2 | 99 |
| | | truc3 | 98 |
+---------+---------+-------+--------------------+
Например, напишем x.field.apply(lambda x: x+1). Возвращается:
Код: Выделить всё
+---------+-------+--------------------+
| machin | truc | a column of series |
+---------+-------+--------------------+
| machin1 | truc1 | 2 |
| | truc2 | 3 |
| | truc3 | 4 |
| | truc4 | 5 |
| machin2 | truc1 | 101 |
| | truc2 | 100 |
| | truc3 | 99 |
+---------+-------+--------------------+
Вот код, который поможет вам воспроизвести мои вычисления< /p>
Код: Выделить всё
import pandas as pd
#creation of records
rec=[{'machin':'machin1',
'truc':['truc1','truc2','truc3','truc4'],
'a column':[1,2,3,4]},
{'machin':'machin2',
'truc':['truc1','truc2','truc3'],
'a column':[100,99,98]}]
#creation of pandas dataframe
df=pd.concat([pd.DataFrame(rec[0]),pd.DataFrame(rec[1])])
#creation of multi-index
df.set_index(['machin','truc'],inplace=True)
#creation of a groupby object
x=df.groupby(by='machin')
#rolling computation. Note that to do x.field or x['field'] is the same, and gives same bug as I checked.
x['a column'].rolling(window=5,min_periods=1).mean()
#rolling with apply and lambda, gives same bug
x['a column'].apply(lambda x:x.rolling(window=5,min_periods=1).mean())
#making apply and lambda alone gives no bug
a=x['a column'].apply(lambda x: x+1)
Я пытался сбросить индекс серии , документ здесь.
Код: Выделить всё
a.reset_index(name='machin')
хотя вы можете см. «machin» в значении имени в мультииндексе:
Код: Выделить всё
a.index
MultiIndex(levels=[['machin1', 'machin2'], ['machin1', 'machin2'], ['truc1', 'truc2', 'truc3', 'truc4']],
labels=[[0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2]],
names=['machin', 'machin', 'truc'])
Код: Выделить всё
a.drop(index='machin')
a.drop(index=0)
Мои версии
Python 3.7.1 (по умолчанию, 14 декабря 2018 г., 19:28:38) в среде anaconda, даже в терминал: [GCC 7.3.0] :: Anaconda, Inc. в Linux
pandas 0.23.4
Подробнее здесь: https://stackoverflow.com/questions/555 ... das-object