Issue
I have table taht looks like following
A | B | C
__________
na| 1 | 2
3 | 3 | 4
na| 5 | 6
na| 7 | 8
2 | 9 | 10
na| 11| 12
na| 13| 14
3 | 15| 16
I would like to average all by column A. All the nan values till the number, final outcome should look like
A | B | C
__________
3 | 2 | 3
2 | 7 | 8
3 |13 |14
Solution
df = pd.DataFrame({'A':[pd.NA,3,pd.NA,pd.NA,2,pd.NA,pd.NA,3], 'B':range(1,16,2), 'C':range(2,17,2)})
df.assign(idx = [i if i in df.index[df.A.notna()] else pd.NA for i in df.index]).bfill().groupby('idx').mean().astype(int).reset_index(drop=True)
Output:
A B C
0 3 2 3
1 2 7 8
2 3 13 14
Answered By - René
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.