Python Forum
Optmized way to rewrite this very slow code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optmized way to rewrite this very slow code
#1
Hey folks,

This is my first post so please bear with me. I need some help to optimize the below one liner.

pd_df.loc[flag, 'COL_{}'.format(col_number)] = pd_df.loc[flag, 'COL{}'.format(col_number)].apply(lambda x: x + str(userid) + "@")
pd_df : Panda data frame contains 2M rows
flag= numpy one dimension boolean array to filter/update many rows at once in pd_df
COL_{}'.format(col_number)= Random column number as per main FOR loop like COL_1,COL_5 upto COL_15 (Data type string with 5000 character length)

In general what this code does it, first filter the rows to be updated according to the flag and column to be updated as per column number and append list of user id in those multiple rows and single column with @ as delimiter. For examples @userid1@userid2@userid2 and so one .

This line of code consume 75% of my overall time due to slow pandas data frame loc function and large no of rows i.e 2M.

Can someone please help me to convert this piece into something more optimized way like dictionary/numpy data type.

Thanks in advance.

Regards,
Liva
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My python code is running very slow on millions of records shantanu97 7 2,567 Dec-28-2021, 11:02 AM
Last Post: Larz60+
  Cleaner way to rewrite fakka 5 3,061 Dec-05-2019, 04:53 AM
Last Post: stullis
  How do I rewrite this .bat file code onto Python? SteampunkMaverick12 4 2,840 Nov-02-2019, 11:28 PM
Last Post: snippsat
  Slow Python Code Jay123 3 2,477 Sep-09-2019, 08:46 AM
Last Post: Jay123
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 1,966 Feb-26-2019, 02:59 PM
Last Post: nikos
  simple code is way too slow JAREDZ 7 8,070 Nov-11-2018, 12:03 PM
Last Post: Larz60+
  piexif rewrite exif data yawstick 2 3,312 Oct-09-2018, 08:56 PM
Last Post: yawstick
  Not having to rewrite 'obj.' on each line beuaaa 4 3,147 Sep-30-2018, 02:21 AM
Last Post: micseydel
  How to rewrite image file name based on ocr data.txt kevinchr 0 3,639 Apr-16-2018, 07:09 PM
Last Post: kevinchr
  how do i rewrite this code to give me 10 outputs BlackPimpernel 2 2,660 Mar-29-2018, 11:29 AM
Last Post: BlackPimpernel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020