1

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

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252

1 Answers1

0

You can update original list with test if exist and not exist value, also remove with append working inplace for update original list, so soluton is:

#for replace NaNs
df = df.fillna('')

original = ['AAPL', 'AAL', 'MPWR', 'TRMB']

#looping by df simplier way
for a, r in df[['added','removed']].to_numpy():
    if a != '' and a in original:  
        original.remove(a)
    elif r != '' and r not in original:
        original.append(r)
        #alternative instead append
        #original = original + [r] 

print (original)
['AAPL', 'AAL', 'FTI', 'CXO', 'TIF']
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I think your old answer, the one with original = original + [r] is better. It worked on my end, while your present solution, the one with append, doesn't. Nevertheless, thank you! :) – finmathstudent Apr 09 '21 at 07:30