Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Fill in NaN values in dataframe intervals according to conditions of a column

I have a dataframe with me which has an ID column and description column with START and STOP values represented by the ID's. Every START-STOP pair is denoted by an ID and it is incremented to 1 on next appearance of the pair.

Initial Dataframe

I need to increment the ID by 1 right after the STOP element has occured (update the NaN's of course) and this same ID should continue till I find the next STOP. Also how to take care of the first START-STOP pair since the main problem focuses on covering STOP-STOP event? Also, any number of events or segments can be there inside the START-STOP or a STOP-STOP pair

I would like to have like this in the end

output dataframe

This kind of needs to be applied for hundreds of thousands of rows and not a bunch of rows shown as sample. Kindly help me on this! Thanks in advance :)

question from:https://stackoverflow.com/questions/66059546/fill-in-nan-values-in-dataframe-intervals-according-to-conditions-of-a-column

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

1 Reply

0 votes
by (71.8m points)
# create a group-tag by every STOP
cond = df.SEG_DESC == 'STOP'
df['tag'] = cond.cumsum()
df.loc[cond, 'tag'] = df.loc[cond, 'tag'] - 1

# for every tag-group use back fillna
df['ID_START_STOP'] = df.groupby('tag')['ID_START_STOP'].bfill().astype(int)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...