Pandas dataframe first instance of value in column

I have a df:

                     Voltage
01-02-2017 00:00       13.1
01-02-2017 00:01       13.2
01-02-2017 00:02       13.3
01-02-2017 00:03       14.1
01-02-2017 00:04       14.3
01-02-2017 00:04       13.5

I need the time (hh: mm) of the first instance when the value in the Voltage column> = 14.0. There should be only one time value in the "Full Charge Time" column.

                     Voltage   Time of Full Charge
01-02-2017 00:00       13.1
01-02-2017 00:01       13.2
01-02-2017 00:02       13.3
01-02-2017 00:03       14.1         00:03
01-02-2017 00:04       14.3
01-02-2017 00:04       13.5

I'm trying something like that, but can't figure it out:

df.index = pd.to_datetime(df.index)
df.['Time of Full Charge'] = np.where(df.['Voltage'] >= 14.0), (df.index.hour:df.index.minute))    
+4
source share
2 answers

You need idxmaxfor the first index value by condition, only the required index must be unique:

idx = (df['Voltage'] >= 14.0).idxmax()
df.loc[mask, 'Time of Full Charge'] = mask.idxmax().strftime('%H:%M')
print (df)
                     Voltage Time of Full Charge
2017-01-02 00:00:00     13.1                 NaN
2017-01-02 00:01:00     13.2                 NaN
2017-01-02 00:02:00     13.3                 NaN
2017-01-02 00:03:00     14.1               00:03
2017-01-02 00:04:00     14.3                 NaN
2017-01-02 00:04:00     13.5                 NaN

Or:

idx = (df['Voltage'] >= 14.0).idxmax()
df['Time of Full Charge'] = np.where(df.index == idx, idx.strftime('%H:%M'), '')
print (df)
                     Voltage Time of Full Charge
2017-01-02 00:00:00     13.1                    
2017-01-02 00:01:00     13.2                    
2017-01-02 00:02:00     13.3                    
2017-01-02 00:03:00     14.1               00:03
2017-01-02 00:04:00     14.3                    
2017-01-02 00:04:00     13.5     

For a unique index, you can use MultiIndex:

df.index = [np.arange(len(df.index)), df.index]

idx = (df['Voltage'] >= 14.0).idxmax()
df['Time of Full Charge'] = np.where(df.index.get_level_values(0) == idx[0], 
                                     idx[1].strftime('%H:%M'),
                                     '')

df.index = df.index.droplevel(0)
print (df)
                     Voltage Time of Full Charge
2017-01-02 00:00:00     13.1                    
2017-01-02 00:01:00     13.2                    
2017-01-02 00:02:00     13.3                    
2017-01-02 00:03:00     14.1               00:03
2017-01-02 00:04:00     14.3                    
2017-01-02 00:04:00     13.5                    
+4
source

You can use numpy.searchsorted () if the column is sorted Voltage:

In [260]: df.index[np.searchsorted(df.Voltage, 14)]
Out[260]: DatetimeIndex(['2017-01-02 00:03:00'], dtype='datetime64[ns]', freq=None)
+2

Source: https://habr.com/ru/post/1675707/


All Articles