What I'm trying to do is:
df = pd.DataFrame({"Person":["John", "Myla", "Mary", "John", "Myla"],
"Age1": [24, 6, 21, 33, 17],
"Single": [False, True, True, True, False]})
Age1 Person Single
0 24 John False
1 6 Myla True
2 21 Mary True
3 33 John True
4 17 Myla False
From the above dataframe, I need to add a new column that will show "1" if the the age if the person is above the age of another dataframe.
My second dataframe is this:
df2 = pd.DataFrame({"Person":["John", "Myla", "Mary"],
"Age1": [25, 10, 15] }).set_index(['Person'])
Age1
Person
John 25
Myla 10
Mary 15
The results ideally would be:
Age1 Person Single Final
0 24 John False 0
1 6 Myla True 0
2 21 Mary True 1
3 33 John True 1
4 17 Myla False 1
So, all John's in my first table with an age greater that 25 in the second table will be given a 1, otherwise 2.
I tried doing it with a dictionary,
a=df2.set_index('Person').T.to_dict('list')
but my dictionary looks like this:
{'John': [24L], 'Mary': [15L], 'Myla': [0L]}
but it's getting me really confused. I also thought about merging the 2 dataframes and then comparing to create the new column, but is there any other more straight option?
Or if I manage to convert the 2nd ddataframe in a dictionary, I could use map df['Final'] = df.Person.map(dictionary) but still shouldn't compare the ages. Please help.