I would like a review regarding the method I use to create the new columns and then reposition them in the correct place where they should be.
The new column called total_matched_vls
should always be on the right side of the column called total_matched
, which in the original CSV is at position 10
The new column called market_matched_vls
should always be on the right side of the column called market_matched
, which in the original CSV is at position 12
import pandas as pd
def calc_back(df):
if (df['result'] == 'WINNER'):
return (df['odds']-1)*0.935
elif (df['result'] == 'LOSER'):
return -1
else:
return ''
def calc_lay(df):
if (df['result'] == 'LOSER'):
return (1/(df['odds']-1))*0.935
elif (df['result'] == 'WINNER'):
return -1
else:
return ''
def total_matched(df):
if (df['total_matched'] < 1000):
return 'centenas'
elif (df['total_matched'] >= 1000) and (df['total_matched'] < 100000):
return 'milhares'
elif (df['total_matched'] >= 100000) and (df['total_matched'] < 1000000):
return 'centenas de milhares'
elif (df['total_matched'] >= 1000000):
return 'milhões'
else:
return ''
def market_matched(df):
if (df['market_matched'] < 1000):
return 'centenas'
elif (df['market_matched'] >= 1000) and (df['market_matched'] < 100000):
return 'milhares'
elif (df['market_matched'] >= 100000) and (df['market_matched'] < 1000000):
return 'centenas de milhares'
elif (df['market_matched'] >= 1000000):
return 'milhões'
else:
return ''
filials = [
[
r'C:\Users\Computador\Desktop\csv\history.csv',
'history_p&l.csv'
],
[
r'C:\Users\Computador\Desktop\csv\results.csv',
'results_p&l.csv'
]
]
for a,b in filials:
df = pd.read_csv(a, dtype={'market_id': str})
df['back'] = df.apply(calc_back, axis=1)
df['lay'] = df.apply(calc_lay, axis=1)
df['total_matched_vls'] = df.apply(total_matched, axis=1)
df['market_matched_vls'] = df.apply(market_matched, axis=1)
col = df.pop("total_matched_vls")
df.insert(11, col.name, col)
col_2 = df.pop("market_matched_vls")
df.insert(14, col_2.name, col_2)
df.to_csv(b, index=False)
A brief example of the original CSV for ease of testing:
open_local_data,country,competition,match_id,match_name,market_id,market_name,runner_id,runner_name,status,total_matched,odds,market_matched,percentage,above_odds,result
2022-08-24 15:00:00,UY,Uruguayan Segunda Division,31684262,Uruguay Montevideo FC v Progreso,1.202440748,Match Odds,11076801,Uruguay Montevideo FC,OPEN,197.2,2.88,448.52,43.96682422188531,9.24460199966309,WINNER
2022-08-24 15:00:00,AT,Austrian Matches,31685733,SV Gerasdorf Stammersdorf v Dinamo Helfort,1.202453470,Match Odds,10299781,SV Gerasdorf Stammersdorf,OPEN,15.99,3.05,27.12,58.96017699115043,26.17329174524879,LOSER
2022-08-24 15:00:00,UY,Uruguayan Segunda Division,31684267,Villa Espanola v Sud America,1.202440560,Match Odds,58805,The Draw,OPEN,458.35,3.5,651.11,70.39517132281796,41.82374275138939,LOSER
2022-08-24 15:00:00,UY,Uruguayan Segunda Division,31684266,Miramar Misiones v Central Espanol,1.202440654,Match Odds,5300627,Miramar Misiones,OPEN,642.05,2.1,1075.66,59.68893516538684,12.069887546339224,LOSER