I am trying to select certain inputs that match my conditions of other columns and create a new column with that value.
I have tried .merge
, .loc
ifelse
and I continue to have something wrong.
I have a dataframe with tickers as the index and the following columns for multiple stocks and option data ['date', 'price', 'exdate', 'position']
If the condition is met of having a '1' in the position column, I want to look in the table and obtain the 'price' on the 'date' that would be equal to the 'exdate' and display it in a new column to the right. The price should be in the same price as the 1. Short example:
import numpy as np
import pandas as pd
t = pd.DataFrame(index=['AAPL','AAPL','AAPL','AAPL', 'AMZN', 'AMZN', 'AMZN', 'AMZN'])
t['Date'] = ['12/01/2017','12/02/2017','12/03/2017','12/04/2017','12/01/2017','12/02/2017','12/03/2017','12/04/2017']
t['Price'] = [125.5, 123.1, 126.4, 128.9, 431.2, 433.5, 432.6, 444.0]
t['Exdate'] = ['12/04/2017','12/04/2017','12/04/2017','12/04/2017','12/04/2017','12/04/2017','12/04/2017','12/04/2017']
t['Position'] = [1,0,0,0,0,0,1,0]
t