in Education by
I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown'], 'Date0': ['01/01/1999','01/06/1999','01/01/1979'], 'Age0': [29,44,21], 'Date1': ['08/01/1999','07/01/2014','01/01/2016'],'Age1': [35, 45, 47], 'Date2': [None,'01/06/2035','08/01/1979'],'Age2': [47, None, 74], 'Last_age': [47,45,74]}) I would like to add new column to get the date corresponding to the value presents in 'Last_age' for each row to get something like that : df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown'], 'Date0': ['01/01/1999','01/06/1999','01/01/1979'], 'Age0': [29,44,21], 'Date1': ['08/01/1999','07/01/2014','01/01/2016'],'Age1': [35, 45, 47], 'Date2': [None,'01/06/2035','08/01/1979'],'Age2': [47, None, 74], 'Last_age': [47,45,74], 'Last_age_date': ['Error no date','07/01/2014','08/01/1979']}) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I will just using wide_to_long reshape your df s=pd.wide_to_long(df.reset_index(),['Date','Age'],i=['Last_age','index'],j='Drop') s.loc[s.Age==s.index.get_level_values(0),'Date'] Out[199]: Last_age index Drop 47 0 2 None 45 1 1 07/01/2014 74 2 2 08/01/1979 Name: Date, dtype: object df['Last_age_date']=s.loc[s.Age==s.index.get_level_values(0),'Date'].values df Out[201]: Last_Name Date0 Age0 ... Age2 Last_age Last_age_date 0 Smith 01/01/1999 29 ... 47.0 47 None 1 None 01/06/1999 44 ... NaN 45 07/01/2014 2 Brown 01/01/1979 21 ... 74.0 74 08/01/1979 [3 rows x 9 columns]

Related questions

0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have a dataset |category| cat a cat b cat a I'd like to be able to return something like (showing unique values ... cat a 2 cat b 1 Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I have three columns like follows: Sales Population Income Price 14 48777.0 285.0 nan 17 12550.0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    In [60]: print(row.index) Int64Index([15], dtype='int64') I already know that the row number is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I don ... 'gdp', 'cap'] Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How to delete a column in a DataFrame, currently I am using: del df['column_1'] this is working fine, ... expected df.column_name ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
...