Python 在“日期”列中按日期分组

Python 在“日期”列中按日期分组,python,pandas,Python,Pandas,我想计算超过滚动平均值的股票百分比,因此,我需要按“日期”对数据进行分组,并希望保留“日期”列。百分比的计算是正确的,但是,我得到的是“NaN”值,而不是实际日期。“日期”列不是数据框索引 def count_number_of_stocks_above_mean(data_frame): data_frame = data_frame.loc[data_frame['Port. Weight'] > 0] #counts number of stocks at given

我想计算超过滚动平均值的股票百分比,因此,我需要按“日期”对数据进行分组,并希望保留“日期”列。百分比的计算是正确的,但是,我得到的是“NaN”值,而不是实际日期。“日期”列不是数据框索引

def count_number_of_stocks_above_mean(data_frame):
    data_frame = data_frame.loc[data_frame['Port. Weight'] > 0]
    #counts number of stocks at given date
    target_df_count_total = data_frame.groupby('Date')['Date', 'Price above Mean'].count().reset_index(level=0, drop=True)
    # counts number of stocks above mean
    target_df_count_price_above_mean = data_frame.groupby('Date')['Date','Price above Mean'].sum().reset_index(level=0, drop=True)
    # calculates % of stocks above mean
    target_df_prct= round((target_df_count_price_above_mean/target_df_count_total),4)
    return target_df_prct
下面是一小块数据:

Date    Ticker  Company Name    Ending Price    Port. Weight    Mean    Price above Mean
1989-02-09  09959910    Borden Inc. 29.25   0.217751540768903   29.01875    1
1989-02-09  205212  ASARCO Incorporated 27.625  0.0580372736732209  28.808333333333334  0
1989-02-09  209399  Burlington Resources Inc.   23.125  0.0 19.054166666666667  1
1989-02-09  219546  Chrysler Corporation    14.375  0.336072750981974   13.885416666666666  1
1989-02-09  231565  Engelhard Corporation   5.92593002319336    0.0448695607192639  5.411111052831014   1
1989-02-09  231752  ENSERCH Corporation 21.375  0.0689363878969973  19.775  1
1989-02-09  236630  General Signal Corporation  25.1875 0.0481599834119684  24.527083333333334  1
1989-02-09  247236  Jefferson-Pilot Corporation 6.51851987838745    0.0622519269932311  6.306173038482666   1
1989-02-09  247680  Johnson Controls, Inc.  2.98957991600037    0.0708125041850512  3.0357636610666914  0
1989-02-09  255096  McDonnell Douglas Corporation   15.0    0.172762591836445   13.70764001210531   1
1989-02-09  262798  NCR Corporation 59.0    0.217124786375439   56.775  1
1989-02-09  268562  Salomon Inc.    26.75   0.167658888987574   26.708333333333332  1
1989-02-09  270750  PSI Resources, Inc. 13.75   0.037335682884269   13.720833333333333  1
1989-02-09  278731  Sears Roebuck & Co. 42.25   0.725775907049812   41.791666666666664  1
1989-02-09  288080  Teledyne Inc.   70.5    0.195999964491311   68.89666646321615   1
1989-02-09  292436  Upjohn Company  31.5    0.292572776505453   29.966666666666665  1
1989-02-09  3614B   Citicorp    27.125  0.44169960825601    26.575  1
1989-02-09  37011810    General Instrument Corporation  32.0    0.0438521329982094  28.029166666666665  1
1989-02-09  45067910    ITT Corporation 54.125  0.351136727389851   52.99166666666667   1
1989-02-09  45850610    Interco Inc.    3.5 0.00673786072637269 3.154166666666667   1
1989-02-09  7448B   Security Pacific Corporation    38.625  0.219156961706361   37.05   1
1989-02-09  84610410    Sovran Financial Corporation    34.625  0.103973459146259   34.32083333333333   1
1989-02-09  AA  Aluminum Company of America 71.2968978881836    0.279191034781877   68.35312296549479   1
1989-02-09  AAL.1   Alexander & Alexander Services Inc. 23.375  0.0472567527053728  23.441666666666666  0
1989-02-09  AAMRQ   AMR Corporation 29.375  0.183440847736901   27.822916666666668  1
1989-02-09  AAPL    Apple Computer Inc. 1.36607003211975    0.241179618711581   1.4497020006179804  0
1989-02-09  ABI The Perkin-Elmer Corporation    6.0 0.0522505023153442  6.214583333333334   0
1989-02-09  ABS.1   Albertson's Inc.    10.375  0.139523284517948   9.958334986368817   1
1989-02-09  ABT Abbott Laboratories 6.1875  0.551759320307428   6.022394720713297   1
1989-02-09  ACKH    Armstrong World Industries, Inc.    36.0    0.0806460223676931  34.74166666666667   1
1989-02-09  ACV.I   Alberto-Culver Company  6.20833015441895    0.0244739725185989  5.9631937026977555  1
1989-02-09  ACY.1   American Cyanamid Co.   50.125  0.237452270838345   49.32083333333333   1
1989-02-09  ADM Archer-Daniels-Midland Company  5.47999000549316    0.208155885155713   5.261965958277385   1
1989-02-09  ADP Automatic Data Processing, Inc. 5.0 0.152894482282014   4.94531265894572    1
1989-02-09  ADSK    Autodesk, Inc.  4.0 0.03880949782187    3.7828126668930055  1
1989-02-09  AEE Union Electric Company  24.125  0.0 23.85   1
1989-02-09  AEP American Electric Power Company, Inc.   26.5    0.257303047425092   27.066666666666666  0
1989-02-09  AET Aetna Life and Casualty Company 12.5625 0.281776485499235   12.1895866394043    1
1989-02-09  AGC.1   American General Corporation    8.21875 0.200201232527231   8.004166666666666   1
1989-02-09  AGREA   American Greetings Corporation Class A  11.25   0.035879324892007   11.06875    1
1989-02-09  AHM.1   H.F. Ahmanson & Company 18.375  0.0910737143332232  17.345833333333335  1
1989-02-09  AIG American International Group, Inc.  123.470001220703    0.601267211039901   119.82483393351244  1
1989-02-09  AIT.1   Ameritech Corporation   12.9062004089355    0.69722254446526    12.284389877319347  1
1989-02-09  AL.1    Alcan Inc.  23.0    0.263589509788732   22.880560048421227  1
1989-02-09  AMD Advanced Micro Devices, Inc.    4.4375  0.0361345930657677  4.460416666666666   0
1989-02-09  AMGN    Amgen Inc.  0.8125  0.0 0.732291603088379   1
1989-02-09  AMH.1   Amdahl Corporation  19.75   0.107689221433691   20.1    0
1989-02-09  AMP.2   AMP Incorporated    23.875  0.257394412976269   23.0    1
1989-02-09  AMT.1   Acme-Cleveland Corporation  10.5    0.00331396950358141 9.908333333333333   1
1989-02-09  AMX.2   Amax Inc.   26.75   0.115624225312409   25.3625 1
1989-02-09  AN.2    Amoco Corporation   19.3125 0.995003885689492   19.239583333333332  1
1989-02-09  ANDW    Andrew Corp.    2.09876990318298    0.0107708198213365  1.9514409859975188  1
1989-02-09  ANV.1   TRINOVA Corporation 28.625  0.0490902432651675  28.204166666666666  1
1989-02-09  APD Air Products and Chemicals, Inc.    11.6875 0.128653479390695   11.278123346964517  1
1989-02-09  ARB Ceridian Corporation    50.0    0.0419296361647271  51.302083333333336  0
1989-02-09  ARC.3   Atlantic Richfield Company  42.875  0.716223838337686   41.920833333333334  1
1989-02-09  AS  Armco Inc.  11.5    0.0510253646649544  10.8875 1
1989-02-09  ASC.1   American Stores Co. 7.20312023162842    0.0996186239178344  7.315625    0
1989-02-09  ASH Ashland Oil, Inc.   34.125  0.0995478842761234  34.483333333333334  0
1989-02-09  AVATQ   National Intergroup Inc.    19.0    0.0207277690301599  19.258333333333333  0
1989-02-09  AVP Avon Products, Inc. 2.71875 0.058790918445647   2.634374666213989   1
1989-02-09  AVY Avery Dennison Corporation  11.5    0.0509572824728723  11.633333333333333  0
1989-02-09  AXP American Express Company    10.4167003631592    0.653968494064866   9.608333651224772   1
1989-02-09  AZA.1   ALZA Corporation    6.59375 0.0 6.230208333333334   1
1989-02-09  BA  Boeing Company  13.9443998336792    0.48364995543785    13.655550003051761  1
1989-02-09  BAC NationsBank Corporation 7.96875 0.159107624872378   7.25625 1
1989-02-09  BAC.Z.  BankAmerica Corporation 10.75   0.223098024073281   9.679166666666667   1
1989-02-09  BAX Baxter International Inc.   10.0625 0.251021819724081   9.516666666666667   1
1989-02-09  BBI.2   Barnett Banks Inc.  16.875  0.106395149465768   17.1125 0
1989-02-09  BC  Brunswick Corporation   18.5    0.0816369190087063  18.158333333333335  1
1989-02-09  BCO The Pittston Company    18.875  0.0355503511201737  18.045833333333334  1
1989-02-09  BCR C. R. Bard, Inc.    11.375  0.0627072141044906  11.297916666666667  1
1989-02-09  BDK Black & Decker Corporation  24.5    0.0723577238319154  23.770833333333332  1
1989-02-09  BDX Becton, Dickinson and Company   6.84375 0.104951662347605   6.637499682108561   1
1989-02-09  BEAM    American Brands, Inc.   32.0625 0.309139254049012   31.991666666666667  1
1989-02-09  BEV Beverly Enterprises Inc.    7.875   0.0213408888901008  6.916666666666667   1
1989-02-09  BF.B    Brown-Forman Corporation Class B    2.32444000244141    0.0917630551928634  2.140445637702943   1
1989-02-09  BFI Browning-Ferris Industries Inc. 30.0    0.223657008417641   28.495833333333334  1
1989-02-09  BFO.2   CPC International Inc.  13.0937995910645    0.204195447953583   13.254173278808599  0
1989-02-09  BGG Briggs & Stratton Corporation   7.0 0.0203182344760629  7.123958333333333   0
1989-02-09  BHGE    Baker Hughes Inc.   14.5    0.0867463801499602  14.741666666666667  0
1989-02-09  BHMSQ   Bethlehem Steel Corp.   27.25   0.102566672200508   25.054166666666667  1
1989-02-09  BKB.2   Bank of Boston Corporation  12.6875 0.09064500805691    12.1625 1
1989-02-09  BLL Ball Corporation    1.65625 0.0295692085114477  1.6848960002263387  0
1989-02-09  BLS BellSouth Corporation   10.5937995910645    1.02371887326721    10.205209986368818  1
1989-02-09  BLY Bally Manufacturing Corporation 24.125  0.0336752484062539  22.7125 1
1989-02-09  BMET    Biomet Inc. 2.07406997680664    0.0 1.8774690270423904  1
1989-02-09  BMS Bemis Company, Inc. 6.46875 0.0333815440279137  6.159375    1
1989-02-09  BMY Bristol-Myers Squibb Company    11.6562004089355    1.21979276588525    11.280200068155915  1
1989-02-09  BNI Burlington Northern Inc.    8.29166984558106    0.0943037741233582  9.116664950052897   0
1989-02-09  BNL.1   Beneficial Corporation  23.0625 0.0514116221037734  22.50625    1
1989-02-09  BNSSA   BNS Holding Inc. Cl A   1712.5  0.00390742412562617 1732.5  0
1989-02-09  BOAT.   Boatmen's Bancshares Inc.   15.9375 0.0 15.402083333333334  1
1989-02-09  BOL Bausch & Lomb Inc.  23.0625 0.0690864662075295  21.835416666666667  1
1989-02-09  BRNOQ   Bruno's, Inc.   11.5    0.0 10.5375 1
1989-02-09  BSET    Bassett Furniture Industries, Inc.  20.9333000183105    0.0162415623077136  20.995553207397457  0
1989-02-09  BT.2    Bankers Trust Corporation   39.0    0.160023263334134   37.7    1
1989-02-09  BUD.2   Anheuser-Busch Companies Inc.   8.4375  0.478800185278591   8.090625    1
1989-02-09  BV.2    Blockbuster Entertainment Corporation   5.875   0.0 5.580208333333333   1
1989-02-09  BWY.1   Carter Hawley Hale Stores, Inc. 106.432998657227    0.0122959357274386  108.48949839274101  0
1989-02-09  C   Primerica Corp. 20.4167003631592    0.13856722834628    18.83332659403483   1
1989-02-09  CA  Computer Associates International, Inc. 5.46295976638794    0.17169934205776    5.1648139476776125  1
1989-02-09  CAG Conagra Brands, Inc.    6.83333015441895    0.12404529960465    6.78888799349467    1
1989-02-09  CAL Brown Group, Inc.   15.1111001968384    0.0297160772728691  14.801863352457678  1
1989-02-09  CAT Caterpillar Inc.    7.625   0.310358043053793   7.915104643503825   0
1989-02-09  CB.1    Chubb Corporation   8.0 0.136737092082188   7.539063294728597   1
1989-02-09  CBB.1   Roadway Services, Inc.  32.5    0.0638555756823075  30.904166666666665  1
1989-02-09  CBE Cooper Industries PLC Cl A  14.6875 0.16005277649102    14.225006612141934  1
1989-02-09  CBL.1   Corroon & Black Corporation 32.75   0.0283804074567535  31.683333333333334  1
1989-02-09  CBS.1   CBS Inc.    34.7000007629395    0.205833021690685   34.72416648864747   0
1989-02-09  CBS.2   Westinghouse Electric Corporation   27.9375 0.405990249330451   27.145833333333332  1
1989-02-09  CCB.2   Capital Cities/ABC Inc. 36.4500007629395    0.329143629477954   36.32791671752931   1
1989-02-09  CCK Crown Cork & Seal Company, Inc. 15.75   0.0619816582811502  16.126386642456055  0
1989-02-09  CCTYQ   Circuit City Stores, Inc.   5.1875  0.0470893436176551  4.939062976837159   1
1989-02-09  CEG Baltimore Gas and Electric Company  20.75   0.125449770549903   20.908329963684082  0
1989-02-09  CEM.2   First Mississippi Corporation   18.125  0.0179636490478221  17.679166666666667  1
1989-02-09  CFL.1   CoreStates Financial Corp.  21.375  0.0 20.364583333333332  1
1989-02-09  CGP.1   Coastal Corporation 12.0    0.122150222444316   11.650003369649253  1
1989-02-09  CHA.3   Champion International Corporation  34.0    0.162719460222096   32.9625 1
1989-02-09  CHRS.1  Charming Shoppes    8.375   0.0419875211279852  8.058333333333334   1
1989-02-09  CI  Cigna Corporation   5.9861102104187 0.212443665702546   5.610185368855794   1
1989-02-09  CIC.3   Continental Corporation 34.375  0.0929356545198307  33.541666666666664  1
1989-02-09  CKL Clark Equipment Company 35.0    0.0301528576533166  32.88333333333333   1
1989-02-09  CL  Colgate-Palmolive Company   2.91406011581421    0.161740631945346   2.8796873490015664  1
1989-02-09  CLX Clorox Company  8.40625 0.093453570890967   8.079166666666667   1
1989-02-09  CMB.1   Chase Manhattan Corporation 31.75   0.165914463503386   30.595833333333335  1
1989-02-09  CMCSA   Comcast Corporation Class A 4.30556011199951    0.0727171837349436  4.065740338961283   1
1989-02-09  CMCSK   Comcast Corporation 2.79629993438721    0.0 2.664197293917339   1
1989-02-09  CMI Cummins Inc.    8.23437976837158    0.0343380573052872  8.061458333333333   1
1989-02-09  CNA CNA Financial Corporation   19.0573997497559    0.183978027660471   19.303053601582846  0
1989-02-09  CNG Consolidated Natural Gas Company    39.75   0.16463220292242    40.25416666666667   0
1989-02-09  CNP Houston Industries Inc. 14.0    0.176851809165661   14.122916666666667  0
1989-02-09  CNW CNF, Inc.   35.5    0.0623035079736531  34.2    1
1989-02-09  COP Phillips Petroleum Company  10.4375 0.255480612798307   10.395833333333334  1
1989-02-09  COST    Price Costco Inc.   3.0 0.0 2.888193988800049   1
1989-02-09  CPB Campbell Soup Company   7.6875  0.199902975031127   7.873958333333333   0
1989-02-09  CPQ.2   Compaq Computer Corporation 2.45000004768372    0.144168468161348   2.2050003528594972  1
1989-02-09  CR  Crane Co.   7.70370006561279    0.0275577572916504  7.181481997172038   1
1989-02-09  CRR.3   Conrail Inc.    18.25   0.125474684243604   17.752083333333335  1
1989-02-09  CSR.1   Central and South West Corporation  15.5    0.146397574691651   15.879166666666666  0
1989-02-09  CSX CSX Corporation 2.76042008399963    0.162611702393541   2.7236099720001237  1
1989-02-09  CTB Cooper Tire & Rubber Company    6.46875 0.0 5.948958333333334   1
1989-02-09  CTCO.1  Cross & Trecker Corporation 14.75   0.00916415631506231 15.25   0
1989-02-09  CTX Centex Corp.    3.57811999320984    0.0214711484180796  3.609373998641968   0
1989-02-09  CUE.1   Quantum Chemical Corporation    56.785099029541 0.0723993908092653  66.92875633239748   0
1989-02-09  CVGYQ   Ogden Corporation   29.375  0.0587044902461742  30.116666666666667  0
1989-02-09  CVN.3   C&S Sovran Corporation  26.625  0.0 25.366666666666667  1
1989-02-09  CVS Melville Corporation    10.0312004089355    0.21934796923864    9.745833333333335   1
1989-02-09  CVX Chevron Corporation 12.1562004089355    0.834571703953511   12.075009918212901  1
1989-02-09  CYM.1   Cyprus Amax Minerals Company    26.0832996368408    0.0504931397856913  24.044436581929524  1
1989-02-09  CYR.1   Cray Research Inc.  61.125  0.0896891120382095  61.270833333333336  0
1989-02-09  D   Dominion Energy Inc 13.7917003631592    0.20573291393175    14.061109987894698  0
1989-02-09  DALRQ   Delta Air Lines Inc.    27.9375 0.141985861046245   26.491666666666667  1
1989-02-09  DCNAQ   Dana Corp.  19.8125 0.0808008517680322  19.704166666666666  1
1989-02-09  DD  E. I. du Pont de Nemours and Company    16.4792003631592    1.1602971239933 15.795833333333329  1
1989-02-09  DDS Dillard Department Stores, Inc. 15.0832996368408    0.0794672513020937  14.32639338175456   1
1989-02-09  DE  Deere & Company 7.79166984558106    0.177135710513493   7.876388676961264   0
1989-02-09  DEC Digital Equipment Corporation   114.375 0.697395529759543   107.11666666666666  1
1989-02-09  DGN.    Data General Corporation    18.0    0.0263519163363923  18.216666666666665  0
1989-02-09  DI. Dresser Industries Inc. 16.8125 0.114167616801963   15.575  1
1989-02-09  DIGI.1  DSC Communications Corporation  4.0 0.0157929480577047  3.7729166666666667  1
1989-02-09  DIS Walt Disney Company 6.23958015441895    0.505436414689496   5.880208969116213   1
1989-02-09  DJ  Dow Jones & Company Inc.    32.625  0.165044611593755   31.504166666666666  1
1989-02-09  DLX Deluxe Corporation  26.5    0.113529222786301   25.866666666666667  1
1989-02-09  DOV Dover Corporation   7.3125  0.0923147899830392  7.4375  0
1989-02-09  DOW2    Union Carbide Corporation   29.25   0.204388895071749   27.408333333333335  1
1989-02-09  DTE The Detroit Edison Company  18.125  0.133505700125553   17.908333333333335  1
1989-02-09  DUK Duke Energy Corporation 34.0312004089356    0.230548718887156   34.50937334696453   0
1989-02-09  DWDP    Dow Chemical Co.    21.7777996063232    0.902358294717968   20.467589823404957  1
1989-02-09  DXC Computer Sciences Corporation   8.77083015441895    0.0418677886024356  8.272222010294598   1
1989-02-09  DYHGQ   Datapoint Corp. 4.5 0.00227771905955565 4.4375  1
1989-02-09  E.2 Transco Energy Company  38.5    0.0 36.8625 1
1989-02-09  ECH.1   Echlin Inc. 17.0    0.0475481171477016  16.725  1
1989-02-09  ECL Ecolab Inc. 3.45311999320984    0.0380576822723671  3.3270826657613117  1
1989-02-09  ECO.2   Echo Bay Mines Ltd. 15.75   0.0 14.6375 1
1989-02-09  ED  Consolidated Edison Company of New York, Inc.   22.6875 0.259662110758227   23.304166666666667  0
1989-02-09  EFU.1   Eastern Enterprises 29.0    0.0337685686188658  29.0    0
1989-02-09  EIX SCEcorp 16.3125 0.357574028780957   16.358333333333334  0
1989-02-09  EKDKQ   Eastman Kodak Company   47.75   0.777163681004211   46.45   1
1989-02-09  EMR Emerson Electric Co.    7.78125 0.347756266109072   7.752083333333333   1
1989-02-09  ENRNQ   Enron Corp. 4.57812023162842    0.0922954599645256  4.597395038604736   0
1989-02-09  ESY.1   E-Systems Inc.  30.75   0.047831761505683   30.254166666666666  1
1989-02-09  ETN Eaton Corp. 7.32812023162842    0.108732684550305   7.178645356496175   1
1989-02-09  ETR Entergy Corporation 16.375  0.168068422297693   15.975  1
1989-02-09  EXC Philadelphia Electric Company   10.4375 0.219571201035979   10.1375 1
您必须从[]中删除“日期”,您已经按它分组了。不要删除索引,Date是返回数据帧中的新索引,您希望保留它

target_df_count_total = data_frame.groupby('Date')['Price above Mean'].count().reset_index(level=0)

您能发布数据框架吗?给一些示例datagroupby将日期列设置为一个索引级别,并且.reset_indexlevel=0,drop=True会删除该索引级别,删除drop=True可能会解决您的问题。@Swier谢谢。我收到ValueError:无法插入日期,已存在。试图在reset_索引中删除这两个,但仍返回nans。