Skip to content Skip to sidebar Skip to footer

How To Update Some Of The Rows From Another Series In Pandas Using Df.update

I have a df like, stamp value 0 00:00:00 2 1 00:00:00 3 2 01:00:00 5 converting to time delta df['stamp']=pd.to_timedelta(df['stamp']) slicing only odd index

Solution 1:

Try this instead:

df.loc[1::2, 'stamp'] += pd.to_timedelta('30 min')

This ensures you update just the values in DataFrame specified by the .loc() function while keeping the rest of your original DataFrame. To test, run df.shape. You will get (3,2) with the method above.

In your code here:

odd_df=pd.to_timedelta(df[1::2]['stamp'])+pd.to_timedelta('30 min')

The odd_df DataFrame only has parts of your original DataFrame. The parts you sliced. The shape of odd_df is (1,).


Post a Comment for "How To Update Some Of The Rows From Another Series In Pandas Using Df.update"