I have a dataframe df
as follows
index added removed
0 MPWR
1 FTI
2 CXO
3 TRMB
4 TIF
I want to keep a list of names prior to each new addition/ removal.
Suppose originally I have ['AAPL', 'AAL', 'MPWR', 'TRMB']
then on index 0, it should be ['AAPL', 'AAL', 'TRMB']
index 1, ['AAPL', 'AAL', 'TRMB', 'FTI']
index 2, ['AAPL, 'AAL', 'TRMB', 'FTI', 'CXO']
index 3, ['AAPL', 'AAL', 'FTI', 'CXO']
index 4, ['AAPL', 'AAL', 'FTI',' CXO', 'TIF']
i.e. my desired result is
index added removed history
0 MPWR ['AAPL', 'AAL', 'TRMB']
1 FTI ['AAPL', 'AAL', 'TRMB', 'FTI']
2 CXO ['AAPL', 'AAL', 'TRMB', 'FTI', 'CXO']
3 TRMB ['AAPL', 'AAL', 'FTI', 'CXO']
4 TIF ['AAPL', 'AAL', 'FTI', 'CXO', 'TIF']
In essence, this is what I am trying to do,
original = ['AAPL', 'AAL', 'MPWR', 'TRMB']
original.remove(df.iloc[0].added)
original.append(df.iloc[1].removed)
original.append(df.iloc[2].removed)
original.remove(df.iloc[3].added)
original.append(df.iloc[4].removed)
which becomes ['AAPL', 'AAL', 'FTI', 'CXO', 'TIF']
My code is the following:
original = ['AAPL', 'AAL', 'MPWR', 'TRMB']
df = pd.DataFrame({'added':['MPWR','','','TRMB',''], 'removed':['','FTI','CXO','','TIF']})
store = []
for count in range(len(df.index)):
if df.iloc[count].added != '':
new = original.remove(df.iloc[count].added)
elif df.iloc[count].removed != '':
new = original.append(df.iloc[count].removed)
store.append(new)
store
However I run into ValueError: list.remove(x) not in list
Would really appreciate a solution to this issue. I really don't get why the code isn't working... Thanks