简体   繁体   中英

Repeat and fill rows based on column value in pandas?

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'])

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM