Python Forum
pandas values from prior row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas values from prior row
#1
Hello:

I received a list for a user that needs to be reformatted. Need to pluck data out of prior rows.

This code demonstrates the issue:

import  numpy as np
import  os
import  pandas as pd


df  = pd.DataFrame({'Make/Model': ['FORD', 'BRONCO', 'MAVERICK', 'MUSTANG', 'HONDA', 'CIVIC', 'CR-V', 'HYUNDAI', 'TUCSON', 'ELANTRA', 'TOYOTA', 'CAMERY', 'AVALON'],
        'firstYear': [np.nan, 1965.0, 1970.0, 1964.0, np.nan, 1972.0, 1995.0, np.nan, 2004.0, 1990.0, np.nan, 1982.0, 2000.0],
        'style': [np.nan, 'suv', 'compact', 'sport', np.nan, 'compact', 'suv', np.nan, 'suv', 'compact', np.nan, 'sedan', 'sedan'],
        'number doors': [np.nan, '2, 4', '2,4 ', '2', np.nan, '2, 4', '4', np.nan, '4', '4', np.nan, '4', '4'], 
         'manufacturor': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]})



df['manufacturor']   =  np.where(df['firstYear'].shift(1).isna()  &  df['style'].shift(1).isna()    == True  ,
                                   df['Make/Model'].shift(1),  df['manufacturor'].shift(1)   )
The Make/Model attribute contains two data types, need to add the Make to the Model rows.

The above code only adds the Make to the first row of the models.
manufacturor need to be populated for all model rows.

Know I am close, but do not know how to proceed.



Thanks for your attention to this matter.


KD
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Float Slider - Affecting Values in Column 'Pandas' planckepoch86 0 2,399 Jan-22-2022, 02:18 PM
Last Post: planckepoch86
  Sum the values in a pandas pivot table specific columns klllmmm 1 7,425 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 5,026 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  How to compare two columns and highlight the unique values of column two using pandas shubhamjainj 0 5,873 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj
  Do Calculation between Rows based on Column values - Pandas Dataframe ahmedwaqas92 0 2,985 Jan-28-2020, 07:06 AM
Last Post: ahmedwaqas92
  Save all values to pandas of multiple classes jenniferruurs 0 2,702 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  Remove \n from list of values within a pandas columns klllmmm 2 17,342 Jun-24-2019, 05:16 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020