Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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 - Fatal编程技术网

Python 多索引数据帧中两个不同日期的分组和之间的差异

Python 多索引数据帧中两个不同日期的分组和之间的差异,python,pandas,Python,Pandas,我有一个多索引数据框,由三列组成(日期,股票代码,安全说明): 接下来,我想计算今天(示例中为2020-01-02)和昨天之间的差异,或者如果昨天缺失,则计算昨天之前最接近的日期,其中包含股票代码和安全说明的数据 以下是我正在处理的示例数据: df = {'transactionID': {0: 5, 1: 7, 2: 5, 3: 7, 4: 6, 5: 6}, 'date': {0: '1/1/2020', 1: '1/1/2020', 2: '1/2/2020', 3: '1/

我有一个多索引
数据框
,由三列组成(
日期
股票代码
安全说明
):

接下来,我想计算今天(示例中为2020-01-02)和昨天之间的差异,或者如果昨天缺失,则计算昨天之前最接近的日期,其中包含股票代码和安全说明的数据

以下是我正在处理的示例数据:

df = {'transactionID': {0: 5, 1: 7, 2: 5, 3: 7, 4: 6, 5: 6},
 'date': {0: '1/1/2020',
  1: '1/1/2020',
  2: '1/2/2020',
  3: '1/2/2020',
  4: '12/31/2019',
  5: '1/2/2020'},
 'Dollar Gain': {0: 500, 1: 100, 2: -200, 3: -200, 4: -50, 5: 50},
 'Date': {0: '4/24/2018',
  1: '4/24/2018',
  2: '4/24/2018',
  3: '4/24/2018',
  4: '5/24/2019',
  5: '5/24/2019'},
 'Notional': {0: 5, 1: 10, 2: 5, 3: 10, 4: 1, 5: -1},
 'Ticker': {0: 'AAPL', 1: 'MCD', 2: 'AAPL', 3: 'MCD', 4: 'SBUX', 5: 'SBUX'},
 'Security Description': {0: 'AAPL Equity',
  1: 'MCD Equity',
  2: 'AAPL Equity',
  3: 'MCD Equity',
  4: 'SBUX Equity',
  5: 'SBUX Equity'},
 'Price': {0: 100.0, 1: 80.0, 2: 105.5, 3: 105.5, 4: 80.0, 5: 80.0}}
df = pd.DataFrame(df)

df.set_index('date', 'Ticker')
df.groupby(['date','Ticker', 'Security Description']).sum()

下面是我想要的股票输出

  • 2020年1月2日AAPL股票代码美元涨幅为-200。2020年1月1日AAPL股票价格上涨500美元。那么差值是-700
  • 对于SBUX,由于2020年1月1日没有条目,因此使用2019年12月31日(2020年1月1日之前的最近日期)的值,因此50-(-50)=100

编辑


除了能够处理昨天(之前的观察结果),您如何将解决方案推广到月到日、年到日以及任意两个自定义日期之间?

使用
diff
修复您的代码

g=df.groupby(['date','Ticker', 'Security Description']).sum()
ydf=g.groupby(level=1).apply(lambda x : x.diff().sum())
Out[35]: 
        transactionID  Dollar Gain  Notional  Price
Ticker                                             
AAPL              0.0       -700.0       0.0    5.5
MCD               0.0       -300.0       0.0   25.5
SBUX              0.0       -100.0       2.0    0.0

这不管用。SBUX应该是100美元收益和-2名义收益。这也不起作用,因为同一天可能有多个证券为一个单一的股票。可能有“AAPL债务”证券描述与“AAPL股权”的股票代码相同。@cpage sort your date first~:-)排序处理第一条注释,但不处理第二条注释。@cpagec
g.groupby(level=[1,2])。apply(lambda x:x.diff().sum())
g=df.groupby(['date','Ticker', 'Security Description']).sum()
ydf=g.groupby(level=1).apply(lambda x : x.diff().sum())
Out[35]: 
        transactionID  Dollar Gain  Notional  Price
Ticker                                             
AAPL              0.0       -700.0       0.0    5.5
MCD               0.0       -300.0       0.0   25.5
SBUX              0.0       -100.0       2.0    0.0