Stack Overflow Asked on December 25, 2021
I working on a dataframe to generate a iterative value.
For example:
import pandas as pd
import numpy as np
df = pd.DataFrame([[1,1970,np.nan,np.nan],[1,1971,np.nan,np.nan],[1,1972,np.nan,0.081],[1,1973,np.nan,0.222],[1,1974,np.nan,0],
[1,1975,np.nan,0],[1,1976,np.nan,0],[1,1977,np.nan,0],[2,1970,np.nan,np.nan],[2,1971,np.nan,np.nan],[2,1972,np.nan,0.081],[2,1973,np.nan,0.222],[2,1974,np.nan,0],
[2,1975,np.nan,0],[2,1976,np.nan,0],[2,1977,np.nan,0]],columns=['id','t','y','x'])
The iterative fomula is:
y_t = (1 - 0.5) * y_{t-1} + x_t
where the y_0
is the first non-missing X observation within the group times (1 / 0.6)
:
y_0 = non missing value / 0.6.
For the first group, the first non-missing X
value is 0.081
, so the y_0 = 0.081 / 0.6 = 0.135
I have a further question. If the original dataframe is a unbalanced panel. For the group 1, we don’t have the year of 1973 in the dataframe. For the missing year observation, all variables in the year is missing.
For example:
import pandas as pd
import numpy as np
df = pd.DataFrame([[1,1970,np.nan,np.nan],[1,1971,np.nan,np.nan],[1,1972,np.nan,0.081],[1,1974,np.nan,0],
[1,1975,np.nan,0],[1,1976,np.nan,0],[1,1977,np.nan,0],[2,1970,np.nan,np.nan],[2,1971,np.nan,np.nan],[2,1972,np.nan,0.081],[2,1973,np.nan,0.222],[2,1974,np.nan,0],
[2,1975,np.nan,0],[2,1976,np.nan,0],[2,1977,np.nan,0]],columns=['id','t','y','x'])
The desired output is :
id t y x
1 1970 nan nan
1 1971 nan nan
1 1972 0.135 0.081
1 1974 nan 0
1 1975 nan 0
1 1976 nan 0
1 1977 nan 0
2 1970 nan nan
2 1971 nan nan
2 1972 0.135 0.081
2 1973 0.2895 0.222
2 1974 0.14475 0
2 1975 0.072375 0
2 1976 0.0361875 0
2 1977 0.01809375 0
I modified the apply function from blutab’s function , but it doesn’t work?
def rolling_apply(group):
y = []
first_index = group.index[0]
idx=pd.date_range(start=group.index[0], end=group.last_valid_index(), freq='Y')
group=group.reindex(idx)
first_valid_index = group.x.first_valid_index().year - first_index.year
for index, x in enumerate(group.x):
if index < first_valid_index:
y.append(np.nan)
elif index == first_valid_index:
y.append( x/0.6)
else:
temp = (1-0.5)*y[-1] + x
y.append(temp)
group.y = y
#group=group.reset_index()
group=group[group['id'].notnull()]
return group
df = pd.DataFrame([[1,1970,np.nan,np.nan],[1,1971,np.nan,np.nan],[1,1972,np.nan,0.081],[1,1974,np.nan,0],
[1,1975,np.nan,0],[1,1976,np.nan,0],[1,1977,np.nan,0],[2,1970,np.nan,np.nan],[2,1971,np.nan,np.nan],[2,1972,np.nan,0.081],[2,1973,np.nan,0.222],[2,1974,np.nan,0],
[2,1975,np.nan,0],[2,1976,np.nan,0],[2,1977,np.nan,0]],columns=['id','t','y','x'])
df['year']=df['t']
df['month']=12
df['day']=31
df['date']=pd.to_datetime(df[['year','month','day']])
df=df.set_index(df['date'])
df['y'] = df.groupby(df.id).apply(rolling_apply).set_index(df['date']).y
Thanks so much.
To do your rolling apply, you can use pandas.groupby().apply(). Inside the apply you can use a loop to do the calculations per group
def rolling_apply(group):
y = []
first_index = group.index[0]
first_valid_index = group.x.first_valid_index() - first_index
for index, x in enumerate(group.x):
if index < first_valid_index:
y.append(np.nan)
elif index == first_valid_index:
y.append( x/0.6)
else:
temp = (1-0.5)*y[-1] + x
y.append(temp)
group.y = y
return group
df['y'] = df.groupby(df.id).apply(rolling_apply).y
Answered by blutab on December 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP