Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

pandas - How do I assign a name to range of values in the index or in a column?

I'm trying to assign a different to different selections of index positions or timestamp objects in a large data frame. I wrote if statements to label the specific day the spikes occurred in the testing period (see below). I'd like to do something similar with the specific times the spikes occurred on those days; however, the spikes occur at different times.

day_type = []
for row in opt['date']:
    if row == '12/21/2020': day_type.append('Spike Day')
    elif row == '12/22/2020': day_type.append('Spike Day')
    elif row == '12/23/2020': day_type.append('Spike Day')
    elif row == '01/05/2021': day_type.append('Spike Day')          
    else: day_type.append('Idle Day')
opt['day_type'] = day_type
    

I've tried defining one of the desired ranges as a variable.

spike_window_5 = opt.iloc[6283:6296]
    

And a series of objects in a column -->

spike_window_5 = opt['timestamp'].loc['01/05/2021 01:13':'01/05/2021 02:18']
Spike_Window = []
if spike_window_5 in opt.index : Spike_Window.append('Spike 5')
else: Spike_Window.append('Idle')
    
opt['Spike Window'] = Spike_Window

but I get the type error: 'DataFrame' objects are mutable, thus they cannot be hashed.

What can I do differently?

question from:https://stackoverflow.com/questions/65929627/how-do-i-assign-a-name-to-range-of-values-in-the-index-or-in-a-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...