I am reading dataframe and writing exccel, I cannot use direct df.to_excel method as I am writing data in a very particular way.
Looping on dataframe is taking much time, could anything in below snippet be optimized.
When dataframe is having 9000 rows , loop is taking 90 seconds on local machine, please help.
def write_score_data(self,worksheet,year): row=3 col=0 df = self.data_df[self.data_df['YEAR']==year].sort('TYPE') player_list = list(df.PLAYER.unique()) player_list.sort() for player in player_list: df_player=df[df['PLAYER']==player] worksheet.write(row,col,player) col+=1 worksheet.write(row, col, df_player['PLAYER_NAME'].iloc) col+=1 for sa_pair in self.strike_avg: df_pair = df_player[ (df_player['STRIKERATE']==sa_pair) & (df_player['AVERAGE']==sa_pair)] for score_col in ['PREMATCH','MATCH','POSTMATCH']: cell_value = df_pair[score_col] if cell_value.empty: cell_value = ' ' else: cell_value = cell_value.iloc worksheet.write(row,col,cell_value) col +=1 row=row+1 col=0
My Sample dataframe is having structure as -
data_df PLAYER PLAYER_NAME SCORE AVERAGE MATCH POSTMATCH PREMATCH STRIKERATE TYPE YEAR 0 329 Virat Kohli 70 62.89 100 80.0000 60 123.90 ODI 2008