Reputation: 169
I collect data over the course of many days and can elect to say that data in one day should be a repeat of another day. How can I fill in the NaN rows with the data specified by the repeat tag column?
Variation of this question: 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'])
Upvotes: 0
Views: 215
Reputation: 29635
IIUC, you can use map
on repeat_tag with the values from the Series calculation once set_index
Day, and use fillna
to assign the value back to calculation.
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
Upvotes: 1