I am working with a bigger dataframe of which I need to select a time interval, modify the values and update them in this dataframe:
df = pd.DataFrame({'datetime': ['2020-09-01T00:15:00.000000000', '2020-09-01T00:30:00.000000000',
'2020-09-01T00:45:00.000000000', ...,
'2020-09-30T23:30:00.000000000', '2020-09-30T23:45:00.000000000',
'2020-10-01T00:00:00.000000000'],
'p': [407.4 , 410.76, 411.6 , ..., 478.8 , 456.12, 451.08],
'q': [47.88, 52.08, 53.76, ..., 77.28, 68.04, 63.84]})
For that I selected the desired interval from the function pd.between_time
,modified the values and stored them in a smaller dataframe.:
df1 = (df.between_time(start_time='07:15:00',end_time='17:00:00')).reset_index()
Now I would like to "paste" these values into my larger dataframe. How could I do that? I tested the functions pd.uptade
, pd.merge
, pd.join
but I must be applying them incorrectly because there was loss of information:
df.update(df1)
CodePudding user response:
maybe you can use np.where to update the values ? For example, I create new column, but you can overwrite old one:
df.datetime = pd.to_datetime(df.datetime)
df.set_index('datetime', inplace = True)
df['new_p'] = np.where(df.index.isin(df.between_time(start_time='00:15:00',end_time='00:30:00').index), 99999, df['p'])