Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Performance enhancement
#1
Hi, I am new in python programming and I need help to speed up nested loop. Here is what I want to do:
Suppose I have two large size dataframe df1(for price) and df2(for volume) and I want to divide each column into two groups (positive and non-positive), then for each group I call different functions. The direct way to do this is like:

Question1:
(pseudo code):
for c in df1.columns:
    col = df1[c].sort_values(ascending=True)
    pos = col[col>0]
    neg = col[col<=0]
    return func1(pos)+func2(neg)
Question2:
assume both df1 and df2 have the same column names
for c in df1.columns:
    col1 = df1[c]
    col2 = df2[c]
    p1 = col1[col1>0]
    n1 = col1[col1<=0]
    p2 = col2[col2>0]
    n2 = col2[col2<=0]
    return p1.corrwith(p2)+n1.corrwith(n2)

This is time consuming. Is there any smart way to get it run faster? (e.g map, apply, vectorization etc)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  performance kerzol81 1 1,911 Oct-07-2019, 10:19 AM
Last Post: buran
  How I can do performance testing on my API a21250450 0 1,390 Jul-18-2019, 09:29 AM
Last Post: a21250450
  Python performance rvkstudent 4 2,985 Apr-25-2019, 09:29 AM
Last Post: rvkstudent
  Red color enhancement OmarSinno 2 5,267 Oct-24-2017, 08:20 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

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