Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫中多种证券的回报流_Python_Pandas_Time Series - Fatal编程技术网

Python 熊猫中多种证券的回报流

Python 熊猫中多种证券的回报流,python,pandas,time-series,Python,Pandas,Time Series,假设我有一张这样的桌子: Ticker Date ClosingPrice 0 A 01-02-2010 11.4 1 A 01-03-2010 11.5 ... 1000 AAPL 01-02-2010 634 1001 AAPL 01-02-2010 635 换句话说,我们有一系列时间序列,每个股票代码一个时间序列拼接在一起。现在,我想生成一列每日收益。如果我只有一个符号,那么使用pandaspct_chan

假设我有一张这样的桌子:

    Ticker Date       ClosingPrice
0    A     01-02-2010  11.4
1    A     01-03-2010  11.5
           ...
1000 AAPL  01-02-2010  634
1001 AAPL  01-02-2010  635

换句话说,我们有一系列时间序列,每个股票代码一个时间序列拼接在一起。现在,我想生成一列每日收益。如果我只有一个符号,那么使用pandas
pct_change()
函数就很容易了,但是我如何像上面那样对多个时间序列执行此操作(我可以执行一系列GroupBy,将每个GroupBy设置为数据帧,执行返回计算,然后使用
pd.concat()将它们拼接在一起)
但这似乎不是最佳选择。

使用
groupby

df.set_index(['Ticker', 'Date']).ClosingPrice.groupby(level=0).pct_change()

Ticker  Date      
A       01-02-2010         NaN
        01-03-2010    0.008772
AAPL    01-02-2010         NaN
        01-02-2010    0.001577
Name: ClosingPrice, dtype: float64

@谢谢你,很高兴我能帮上忙。