w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Can I apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas?

I think you can use groupby and pct_change (don't blame me for the name..).

First, let's make sure everything's a real time and sort it:

>>> df["TradeDate"] = pd.to_datetime(df["TradeDate"])
>>> df = df.sort(["Symbol", "TradeDate"])
>>> df
   TradeDate Symbol  Close
0 2014-01-01      A  10.00
1 2014-01-02      A  11.00
2 2014-01-03      A  10.50
3 2014-01-01      B   2.00
4 2014-01-02      B   2.10
5 2014-01-03      B   2.05

And then do the work:

>>> df.groupby("Symbol")["Close"].pct_change()
0         NaN
1    0.100000
2   -0.045455
3         NaN
4    0.050000
5   -0.023810
dtype: float64
>>> df["Perf1"] = df.groupby("Symbol")["Close"].pct_change()
>>> df["Perf2"] = df.groupby("Symbol")["Close"].pct_change(2)
>>> df
   TradeDate Symbol  Close     Perf1  Perf2
0 2014-01-01      A  10.00       NaN    NaN
1 2014-01-02      A  11.00  0.100000    NaN
2 2014-01-03      A  10.50 -0.045455  0.050
3 2014-01-01      B   2.00       NaN    NaN
4 2014-01-02      B   2.10  0.050000    NaN
5 2014-01-03      B   2.05 -0.023810  0.025

It would probably be cleaner to do the grouping once, e.g.

grouped = df.groupby("Symbol")["Close"]
for i in range(1,5):
    df["Perf{}".format(i)] = grouped.pct_change(i)

or something.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.