I'm sorting through stock transactions and learning python at the same time. I've read that iterrows isn't always the best, but I struggle to understand how to implement other solutions to my particular situation.
here's what I have, it works and it's faster than what I used to do, but I think it's still slow, what's the fastest way to do this:
data_list = [
['Dividend'],
['Reinvestment'],
['Sell'],
['Withholding']
]
df = pd.DataFrame(data_list,columns = ['buy/sell'])
buysell_list = [
['Dividend','Div'],
['Reinvestment','Div']
]
sort = pd.DataFrame(buysell_list,columns = ['0','1'])
import re
for _, row in sort.iterrows():
#print(row[0],row[1])
df.loc[df['buy/sell'].str.contains(row[0],flags=re.IGNORECASE),'buy/sell'] = row[1]
The result should look like this:
buy/sell
0 Div
1 Div
2 Sell
3 Withholding
thanks for any tips!