简体   繁体   中英

Change one row in a pandas dataframe based on the value of another row

I have a pandas DataFrame with data from an icecream freezer. Several columns describe the different temperatures in the system as well as some other things. One column, named 'Defrost status', tells me when the freezer was defreezing to remove abundant ice with boolean values.

Those 'defrosts' is what I am interested in, so I added another column named "around_defrost". This column currently only has NaN values, but I want to change them to 'True' whenever there is a defrost within 30 minutes away from that specific row in the dataframe. The data is recorded every minute so 30 minutes would mean 30 rows before a defrost and 30 rows behind it need to be set to 'True'

I have tried to do this with itterrows, ittertuples and by playing with the indexes as seen in the figure below but nu success so far. If anyone has a good idea of how this would could be done, I'd really appreciate it!

enter image description here

You need to use dataframe.rolling :

df = df.sort_values("Time") #sort by Time

df['around_defrost'] = df['Defrost status'].rolling(60, center=True, min_periods = 0).apply(
                          lambda x: True if True in x else False, raw=True)

EDIT: you may need rolling(61, center=True) since you want to consider the row in question AND 30 before and after.

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