0

I have a few lines of code that are basically categorizing the video length into different video length brackets, they look like this - but I don't know if I can make them less redundant. It seems very repetitive and maybe it's slower in run time? I'm not sure how this could be improved. Any advice is appreciated. thanks.

df_preroll.loc[(df_preroll['Video length'] > 0) & (df_preroll['Video length'] <= 6), 'Video_Length_Bracket'] = '0-6'  
df_preroll.loc[(df_preroll['Video length'] >= 7) & (df_preroll['Video length'] <= 14), 'Video_Length_Bracket'] = '7-14' 
df_preroll.loc[(df_preroll['Video length'] >= 15) & (df_preroll['Video length'] <= 29), 'Video_Length_Bracket'] = '15-29'
df_preroll.loc[(df_preroll['Video length'] >= 30), 'Video_Length_Bracket'] = '30+' 
df_preroll.loc[(df_preroll['Video length'] == None), 'Video_Length_Bracket'] = None
PiCubed
  • 375
  • 2
  • 5
  • 11
  • 1
    `df_preroll['Video_Length_Bracket'] = pd.cut(df_preroll['Video length'], bins=[0,6,14,29,np.inf], labels=['0-6','7-14','15-29','30+'], right=True)` – jezrael Apr 14 '23 at 05:52
  • how much more efficient this would be compare to the version i have above? Is it still worth the effort to spend the time to convert them say if i have more than 1000 categories? – PiCubed Apr 14 '23 at 06:05
  • Yes, it is more efficient. – jezrael Apr 14 '23 at 06:14

0 Answers0