Python pandas groupby不工作

2024-05-18 16:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个熊猫群对公关股票组。在下面的代码中。为了计算不同的关键业绩指标,股票清单。在这里我只展示了与前一天的不同之处。很明显,我不想让不同的股票持有者之间有区别——这是毫无意义的——因此才有了groupby。但它并没有按预期工作。在

输出文件中出现问题 实际的groupby并没有做它应该做的事情,正如您在下面的输出中看到的那样,列“Difference”违反了groupby(ticker)中的不同组。所以它计算第一个组中最后一个股票代码行和第二个组中第一个股票代码行之间的差异。这不是我们想要的。这条线应该是NaN作为第一行。。。在

这是df Difference中“Difference”列的结果 日期

2015-04-09 NaN
2015-04-10 1.180000
2015-04-13 3.150000
2015-04-14 -0.980000
2015-04-15 1.280000
2015-04-16 -8.280000
2015-04-17 -8.770000
2015-04-09 -139.859995 This is not correct. The groupby does not separate the tickers as it should. This should be a NaN... not the diff between 2 different tickers!

2015-04-10 0.899994
2015-04-13 -1.130005
2015-04-14 -0.589996
2015-04-15 1.000000
2015-04-16 0.350006
2015-04-09 -139.859995

对于为什么groupby中的“Difference”列没有像在我的代码中那样被分离,有什么想法吗?在

^{pr2}$

显然,我的groupby按预期工作,但预期的“差异”列在我的测试输出中不稳定地显示在这里:

(group)
 mmm 

(group (ticker) in df)
            Ticker        Open        High         Low   Adj_Close   Volume
Date                                                                      
2015-04-09    mmm  166.750000  167.500000  166.500000  166.630005  1762800
2015-04-10    mmm  165.630005  167.740005  164.789993  167.529999  1993700
2015-04-13    mmm  167.110001  167.490005  165.919998  166.399994  2022800
2015-04-14    mmm  165.179993  166.550003  164.649994  165.809998  1610300
2015-04-15    mmm  165.339996  167.080002  164.839996  166.809998  2092200
2015-04-16    mmm  165.880005  167.229996  165.250000  167.160004  2721900 

============================================================
(group)
 vws.co 

(group (ticker) in df)
             Ticker   Open   High    Low  Adj_Close   Volume
Date                                                       
2015-04-09  vws.co  315.0  316.1  312.5     311.52  1686800
2015-04-10  vws.co  317.0  319.7  316.4     312.70  1396500
2015-04-13  vws.co  317.9  321.5  315.2     315.85  1564500
2015-04-14  vws.co  320.0  322.4  318.7     314.87  1370600
2015-04-15  vws.co  320.0  321.5  319.2     316.15   945000
2015-04-16  vws.co  319.0  320.2  310.4     307.87  2236100
2015-04-17  vws.co  309.9  310.0  302.5     299.10  2711900
2015-04-20  vws.co  303.0  312.0  303.0     306.49  1629700 

============================================================
df_test (summary from df) (Output)
             Ticker        Open        High         Low   Adj_Close   Volume  Date                                                                          
2015-04-09  vws.co  315.000000  316.100000  312.500000  311.520000  1686800   
2015-04-10  vws.co  317.000000  319.700000  316.400000  312.700000  1396500   
2015-04-13  vws.co  317.900000  321.500000  315.200000  315.850000  1564500   
2015-04-09     mmm  166.750000  167.500000  166.500000  166.630005  1762800   
2015-04-10     mmm  165.630005  167.740005  164.789993  167.529999  1993700   
2015-04-13     mmm  167.110001  167.490005  165.919998  166.399994  2022800   

            Difference  
Date                    
2015-04-09         NaN  
2015-04-10    1.180000  
2015-04-13    3.150000  
2015-04-09 -139.859995  This is not correct!!! This should be NaN...
2015-04-10    0.899994  
2015-04-13   -1.130005 

Tags: dfdategroupnotnanthis股票ticker
1条回答
网友
1楼 · 发布于 2024-05-18 16:17:07

仔细检查代码后,Screener函数中的作用域是错误的。您在该函数中引用df,而没有传递df参数。这意味着它在函数外部的作用域中定义的df变量,即主df。所以您分配的是整个dfdf.diff(),而不是分组的df。在

我发现更容易:

df_ = df.set_index('Ticker', append=True)
df_['Difference'] = df_.groupby(level=1).Adj_Close.diff()
print df_.reset_index('Ticker')

enter image description here

相关问题 更多 >

    热门问题