Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1712
Answered
Charlis
Charlis
Asked: May 12, 20222022-05-12T06:45:19+00:00 2022-05-12T06:45:19+00:00

How to iterate each row according to values of row above?

Suppose sensors are attached to 3 climbers scaling a structure and these sensors capture a certain measurement at random times. The data are captured into the data frame below (the data frame is a lot longer than this):

df = pd.DataFrame({
'Name': ['Cody', 'Dustin', 'Dustin', 'Cody', 'Ryan', 'Dustin', 'Ryan', 'Cody'],
'Timestamp': ['08:10:23', '08:12:58', '08:15:02', '08:19:43', '08:21:00', '08:30:17', '08:34:01', '08:34:59'],
'Category': ['Body Temp', 'Altitude', 'Heart Rate', 'Body Temp', 'Heart Rate', 'Heart Rate', 'Altitude', 'Altitude'],
'Body Temp': [35.9, np.nan, np.nan, 36.2, np.nan, np.nan, np.nan, np.nan],
'Altitude': [np.nan, 7, np.nan, np.nan, np.nan, np.nan, 12, 6],
'Heart Rate': [np.nan, np.nan, 75, np.nan, 71, 69, np.nan, np.nan]
})

     Name Timestamp    Category  Body Temp  Altitude  Heart Rate
0    Cody  08:10:23   Body Temp       35.9       NaN         NaN
1  Dustin  08:12:58    Altitude        NaN       7.0         NaN
2  Dustin  08:15:02  Heart Rate        NaN       NaN        75.0
3    Cody  08:19:43   Body Temp       36.2       NaN         NaN
4    Ryan  08:21:00  Heart Rate        NaN       NaN        71.0
5  Dustin  08:30:17  Heart Rate        NaN       NaN        69.0
6    Ryan  08:34:01    Altitude        NaN      12.0         NaN
7    Cody  08:34:59    Altitude        NaN       6.0         NaN

The intention is to constantly update the measurements at every row, according to each climber and timestamp, such that every succeeding row for each climber will have their measurements updated.

So a result should look something like this:

     Name Timestamp    Category  Body Temp  Altitude  Heart Rate
0    Cody  08:10:23   Body Temp       35.9       NaN         NaN
1  Dustin  08:12:58    Altitude        NaN       7.0         NaN
2  Dustin  08:15:02  Heart Rate        NaN       7.0        75.0
3    Cody  08:19:43   Body Temp       36.2       NaN         NaN
4    Ryan  08:21:00  Heart Rate        NaN       NaN        71.0
5  Dustin  08:30:17  Heart Rate        NaN       7.0        69.0
6    Ryan  08:34:01    Altitude        NaN      12.0        71.0
7    Cody  08:34:59    Altitude       36.2       6.0         NaN

So far I’ve thought of using .sort_value() to separate the climbers and work from there. But I have trouble figuring out how to update each row continually. Are functions or iterrows required for this?

panadaspython
  • 0
  • 1 1 Answer
  • 25 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Best Answer
    Charlis
    2022-05-12T06:46:03+00:00Added an answer on May 12, 2022 at 6:46 am

    The job essentially seems to be filling missing values by a previous value if such value exists at that measurement for each climber, so groupby.ffill should do the job:

    out = df[['Name']].join(df.groupby('Name').ffill())
    

    Output:

         Name Timestamp    Category  Body Temp  Altitude  Heart Rate
    0    Cody  08:10:23   Body Temp       35.9       NaN         NaN
    1  Dustin  08:12:58    Altitude        NaN       7.0         NaN
    2  Dustin  08:15:02  Heart Rate        NaN       7.0        75.0
    3    Cody  08:19:43   Body Temp       36.2       NaN         NaN
    4    Ryan  08:21:00  Heart Rate        NaN       NaN        71.0
    5  Dustin  08:30:17  Heart Rate        NaN       7.0        69.0
    6    Ryan  08:34:01    Altitude        NaN      12.0        71.0
    7    Cody  08:34:59    Altitude       36.2       6.0         NaN
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Ghulam Nabi

    Is this statement, “i see him last night” can be ...

    • 4 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.