I have such a DataFrame (is it a MultiIndexed DataFrame? I am not sure if I understand this correctly...):
df = pd.DataFrame({'index' : range(8),
'variable1' : ["A","A","B","B","A","B","B","A"],
'variable2' : ["a","b","a","b","a","b","a","b"],
'variable3' : ["x","x","x","y","y","y","x","y"],
'result': [1,0,0,1,1,0,0,1]})
df2 = df.pivot_table(values='result',rows='index',cols=['variable1','variable2','variable3'])
variable1 A B
variable2 a b a b
variable3 x y x y x y
index
0 1 NaN NaN NaN NaN NaN
1 NaN NaN 0 NaN NaN NaN
2 NaN NaN NaN NaN 0 NaN
3 NaN NaN NaN NaN NaN 1
4 NaN 1 NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN 0
6 NaN NaN NaN NaN 0 NaN
7 NaN NaN NaN 1 NaN NaN
Now what I want to do is ffill()
the values, but only for variable3 == 'y'
. The desired result is:
variable1 A B
variable2 a b a b
variable3 x y x y x y
index
0 1 NaN NaN NaN NaN NaN
1 NaN NaN 0 NaN NaN NaN
2 NaN NaN NaN NaN 0 NaN
3 NaN NaN NaN NaN NaN 1
4 NaN 1 NaN NaN NaN 1
5 NaN 1 NaN NaN NaN 0
6 NaN 1 NaN NaN 0 0
7 NaN 1 NaN 1 NaN 0
I know I can do this by iterating over variable1
and variable2
, for each of them doing something like:
df2['A']['a']['y'].ffill()
But I guess there should be a method to avoid this...
A little bit tricky because of the need to assign the results.
First swap the levels, putting variable 3 at the top; ffill is then easily computed and assigned back.
In [44]: x = df2.swaplevel('variable1','variable3',axis=1)
In [45]: x['y'] = x['y'].ffill()
In [46]: x.swaplevel('variable3','variable1',axis=1)
Out[46]:
variable1 A B
variable2 a b a b
variable3 x y x y x y
index
0 1 NaN NaN NaN NaN NaN
1 NaN NaN 0 NaN NaN NaN
2 NaN NaN NaN NaN 0 NaN
3 NaN NaN NaN NaN NaN 1
4 NaN 1 NaN NaN NaN 1
5 NaN 1 NaN NaN NaN 0
6 NaN 1 NaN NaN 0 0
7 NaN 1 NaN 1 NaN 0
In 0.13 (coming soon), you can do this
Select the sub-section that we want, and provide drop_level=False
to return it as a full section (eg don't the level that we are selection on), and ffill it.
In [77]: df_sub = df2.xs('y',level='variable3',axis=1,drop_level=False).ffill()
In [78]: df_sub
Out[78]:
variable1 A B
variable2 a b b
variable3 y y y
index
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN 1
4 1 NaN 1
5 1 NaN 0
6 1 NaN 0
7 1 1 0
In [79]: df2.loc[:,df_sub.columns] = df_sub
In [80]: df2
Out[80]:
variable1 A B
variable2 a b a b
variable3 x y x y x y
index
0 1 NaN NaN NaN NaN NaN
1 NaN NaN 0 NaN NaN NaN
2 NaN NaN NaN NaN 0 NaN
3 NaN NaN NaN NaN NaN 1
4 NaN 1 NaN NaN NaN 1
5 NaN 1 NaN NaN NaN 0
6 NaN 1 NaN NaN 0 0
7 NaN 1 NaN 1 NaN 0
there might be a better way of doing this since pandas 0.14.0:
df2.loc[:, (slice(None), slice(None), 'y')] = df2.loc[:, (slice(None), slice(None), 'y')].ffill()
or idx = pd.IndexSlice df2.loc[:, (idx[:,:,'y'])] = df2.loc[:, (idx[:,:,'y'])].ffill()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.