我收集了很多天的数据,可以选择说一天中的数据应该是另一天的重复。如何用重复标记列指定的数据填充NaN行?
这个问题的变化:Repeat sections of dataframe based on a column value
#Example Dataframes
example_data = [[1,np.NaN,"3a+b"],[2,np.NaN,"c"],[3,1,np.NaN],[4,np.NaN,"b+c"], [5,2,np.NaN], [6,0,0]]
to_solve = pd.DataFrame(example_data,columns=['Day','repeat_tag','calculation'])
desired= [[1,np.NaN,"3a+b"],[2,np.NaN,"c"],[3,1,"3a+b"],[4,np.NaN,"b+c"], [5,2,"c"],[6,0,0]]
desired_table=pd.DataFrame(desired,columns=['Day','repeat_tag','calculation'])
答案 0 :(得分:1)
IIUC,您可以在set_index
天将{_3}}应用于重复数列中来自系列计算的值,并使用fillna
将值分配回计算。
to_solve['calculation'] = to_solve['calculation']\
.fillna(to_solve['repeat_tag']\
.map(to_solve.set_index('Day')['calculation']))
print(to_solve)
Day repeat_tag calculation
0 1 NaN 3a+b
1 2 NaN c
2 3 1.0 3a+b
3 4 NaN b+c
4 5 2.0 c
5 6 0.0 0