I have a small script which checks for a condition and if true moves the pandas dataframe row to a new dataframe and then removes the row from the original dataframe.
Originally, I was doing with this regex but this was slow and after some reading on SO I tried it this way - it's slightly quicker.
The production data I'm using runs this across millions of rows so any time saved will be a big help.
Anything I can do to optimise it further?
import pandas as pd
data = [['thomas cook', 222], ['holidays', 333], ['cheap flights', 444], ['thomascook holidays', 555]]
df1 = pd.DataFrame(data, columns=['query', 'clicks'])
df2 = pd.DataFrame(columns=df1.columns)
print(df1)
query clicks
0 thomas cook 222
1 holidays 333
2 cheap flights 444
3 thomascook holidays 555
brand_terms = ['thomas cook', 'thomascook', 'thomas-cook']
for brand_term in brand_terms:
condtion = df1[df1["query"].str.contains(brand_term, case=False, regex=False)]
df2 = df2.append(condtion, ignore_index=True)
df1.drop(condtion.index, inplace=True)
print(df1)
query clicks
1 holidays 333
2 cheap flights 444
print(df2)
query clicks
0 thomas cook 222
1 thomascook holidays 555