You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For performance, I would suggest using the underlying array data and array-slicing as the two columns to be modified come in sequence to use view into it -
a = df.values
df.iloc[:,1:3] = a[:,1:3]/a[:,0,None]
To eloborate a bit more on the array-slicing part, with a[:,[1,2]] would have forced a copy there and would have slowed it down. a[:,[1,2]] on the dataframe side is equivalent to df[['open','close']] and that I am guessing is slowing things down too. df.iloc[:,1:3] is thus improving upon it.
Sample run -
In [64]: df
Out[64]:
prev open close volume
0 20.77 20.87 19.87 962816
1 19.87 19.89 19.56 668076
2 19.56 19.96 20.10 578987
3 20.10 20.40 20.53 418597
In [65]: a = df.values
...: df.iloc[:,1:3] = a[:,1:3]/a[:,0,None]
...:
In [66]: df
Out[66]:
prev open close volume
0 20.77 1.004815 0.956668 962816
1 19.87 1.001007 0.984399 668076
2 19.56 1.020450 1.027607 578987
3 20.10 1.014925 1.021393 418597
In [44]: data = np.random.randint(15, 25, (100000,4)).astype(float)
...: df1 = pd.DataFrame(data, columns=(('prev','open','close','volume')))
...: df2 = df1.copy()
...:
In [45]: %timeit pandas_app1(df1)
...: %timeit numpy_app(df2)
...:
100 loops, best of 3: 2.68 ms per loop
1000 loops, best of 3: 885 µs per loop
For performance, I would suggest using the underlying array data and
array-slicing
as the two columns to be modified come in sequence to use view into it -To eloborate a bit more on the array-slicing part, with
a[:,[1,2]]
would have forced a copy there and would have slowed it down.a[:,[1,2]]
on the dataframe side is equivalent todf[['open','close']]
and that I am guessing is slowing things down too.df.iloc[:,1:3]
is thus improving upon it.Sample run -
Runtime test
Approaches -
Timings -
https://stackoverflow.com/questions/45899613/divide-certain-columns-by-another-column-in-pandas
The text was updated successfully, but these errors were encountered: