读取\u csv()后在数据帧中打印列时出现关键错误

2024-04-19 20:23:07 发布

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

我正在尝试将CSV文件读入pandas数据框并选择一列,但不断收到一个键错误

这是我正在处理的输入文件的一个片段。我想跳过前5行,使用带有model_numberstar_age等作为标题的sitxh行,并收集数据列:

                           1                            2                            3                            4                            5 
              version_number                 initial_mass                    initial_z                    burn_min1                    burn_min2 
                       10398      8.1000000000000005E-001      1.0000000000000000E-003      5.0000000000000000E+001      1.0000000000000000E+003 

                                       1                                        2                                        3                                        4                                        5                                        6                                        7                                        8                                        9                                       10                                       11                                       12                                       13                                       14                                       15                                       16                                       17                                       18                                       19                                       20                                       21                                       22                                       23                                       24                                       25                                       26                                       27                                       28                                       29                                       30                                       31                                       32                                       33                                       34                                       35                                       36                                       37                                       38                                       39                                       40                                       41                                       42                                       43                                       44                                       45                                       46                                       47                                       48                                       49                                       50                                       51                                       52                                       53                                       54                                       55                                       56                                       57                                       58                                       59                                       60                                       61                                       62                                       63                                       64                                       65                                       66                                       67                                       68                                       69                                       70                                       71                                       72                                       73                                       74                                       75                                       76                                       77                                       78                                       79                                       80                                       81                                       82                                       83                                       84                                       85                                       86                                       87                                       88 
                            model_number                                 star_age                                star_mass                           mass_conv_core                             he_core_mass                           he_core_radius                             conv_mx1_top                             conv_mx1_bot                             conv_mx2_top                             conv_mx2_bot                           conv_mx1_top_r                           conv_mx1_bot_r                           conv_mx2_top_r                           conv_mx2_bot_r                            cz_bot_radius                                   log_LH                                  log_LHe                                 log_Teff                               luminosity                                    log_L                                    log_R                                    log_g                          log_surf_cell_z                             log_center_T                           log_center_Rho                             log_center_P                                center_mu                        center_degeneracy                                center_h1                                center_h2                               center_he3                               center_he4                               center_li7                               center_be7                                center_b8                               center_c12                               center_c13                               center_n13                               center_n14                               center_n15                               center_o14                               center_o15                               center_o16                               center_o17                               center_o18                               center_f17                               center_f18                               center_f19                              center_ne18                              center_ne19                              center_ne20                              center_ne22                              center_mg22                              center_mg24                               surface_h1                               surface_h2                              surface_he3                              surface_he4                              surface_li7                              surface_be7                               surface_b8                              surface_c12                              surface_c13                              surface_n13                              surface_n14                              surface_n15                              surface_o14                              surface_o15                              surface_o16                              surface_o17                              surface_o18                              surface_f17                              surface_f18                              surface_f19                             surface_ne18                             surface_ne19                             surface_ne20                             surface_ne22                             surface_mg22                             surface_mg24                                   nu_max                                 delta_nu                                 delta_Pg                          acoustic_cutoff                          acoustic_radius                               d_log_L_dT                          h_exh_core_mass                        h_exh_core_radius 
                                     590                  7.4832653674979620E+003                  8.0999231594859322E-001                  9.3013103578994949E-002                  0.0000000000000000E+000                  0.0000000000000000E+000                  1.1367933255238216E-001                  7.8129078318056600E-008                  1.0000000000000000E+000                  9.9318920202569283E-001                  1.1581183159043519E-001                  9.6384235196922451E-004                  6.9887906688924084E-001                  5.6970970716172920E-001                  0.0000000000000000E+000                 -1.9971124824757927E-001                 -4.8201179092980333E+001                  3.7897634917269190E+000                  6.3290767033577267E-001                 -1.9865964099109620E-001                 -1.5559796749675942E-001                  4.6575661873384373E+000                 -3.0000000000000475E+000                  7.1159454763339030E+000                  1.9980751452335390E+000                  1.7261783473862238E+001                  5.9749025137280032E-001                 -1.3921254545517694E+000                  7.4862988071983971E-001                  6.4224755686599933E-018                  1.1040796520256433E-004                  2.5025816673994183E-001                  3.6245810211769934E-016                  7.0974332935137005E-013                  4.6287532383736027E-023                  1.6090485487993254E-004                  1.9156053663187585E-006                  3.4877009372383987E-016                  6.0985269793407524E-005                  2.2754513772045929E-007                  2.9195681915663947E-031                  8.7420459328129831E-020                  4.6667973725421542E-004                  2.0414721029513937E-007                  1.1381709511386918E-006                  2.2860196785405309E-021                  9.1671666641570419E-023                  2.0874159744382837E-008                  3.7083264706195407E-042                  3.9279368604326808E-040                  1.0269030813449696E-004                  6.7091975862944943E-006                  4.2398886865254202E-017                  2.0006886383171565E-004                  7.4899999942868345E-001                  2.7200701027954921E-017                  2.6605602791999035E-005                  2.4997339496852472E-001                  1.6905661394980510E-037                  1.7567453744563094E-040                  1.0000000000000274E-099                  1.7017919585850694E-004                  1.9011704267928175E-006                  7.2620283197083900E-094                  5.0179160420602355E-005                  2.2876560599578056E-007                  1.0000000000000006E-099                  1.0000000000000006E-099                  4.6668013273267953E-004                  2.0414644161309358E-007                  1.1381848144163238E-006                  1.0000000000000006E-099                  8.4411414882972268E-074                  2.0874146844472571E-008                  1.0000000000000006E-099                  1.0000000000000006E-099                  1.0269030813453584E-004                  6.7091975862945028E-006                  1.1399939856108351E-057                  2.0006886383171633E-004                  4.9774621436997058E+003                  2.1457444791057421E+002                  0.0000000000000000E+000                  4.5146723037873518E+004                  2.3301935755574186E+003                  1.0590627364747510E+001                  0.0000000000000000E+000                  0.0000000000000000E+000 
                                     591                  1.6463183808495516E+004                  8.0999231496783064E-001                  9.2873327284872409E-002                  0.0000000000000000E+000                  0.0000000000000000E+000                  1.1367933269002861E-001                  7.8129078429078902E-008                  1.0000000000000000E+000                  9.9318494262289947E-001                  1.1581293977069999E-001                  9.6385193654037807E-004                  6.9879898141115959E-001                  5.6967302923999130E-001                  0.0000000000000000E+000                 -1.9978213060522462E-001                 -4.8201281132346992E+001                  3.7897483582980382E+000                  6.3267443633058140E-001                 -1.9881971316983504E-001                 -1.5564773672836699E-001                  4.6576657252757956E+000                 -3.0000000000000475E+000                  7.1159439547711134E+000                  1.9980621893502508E+000                  1.7261768696422475E+001                  5.9749042822746268E-001                 -1.3921513883392005E+000                  7.4862947570235450E-001                  6.4224852088404529E-018                  1.1037767369519526E-004                  2.5025860081630319E-001                  3.6293040823707671E-016                  7.1091046311060774E-013                  4.6359908361904593E-023                  1.6088811945050166E-004                  1.9343478788613242E-006                  3.4876029004368768E-016                  6.0986182006780295E-005                  2.2587329315472058E-007                  2.9193215138718783E-031                  8.7416768418526394E-020                  4.6667973812484934E-004                  2.0414813320371832E-007                  1.1381543204624337E-006                  2.2858303888862015E-021                  9.1791850761956234E-023                  2.0874175222493570E-008                  3.7075984662615788E-042                  3.9325993844270721E-040                  1.0269030813445076E-004                  6.7091975862944951E-006                  9.3271194401577135E-017                  2.0006886383171565E-004                  7.4899999942868334E-001                  3.4436613497022586E-017                  2.6605602792180270E-005                  2.4997339496852464E-001                  3.7109055189055696E-037                  1.7477951709463041E-040                  1.0000000000000597E-099                  1.7017919585850675E-004                  1.9011704267928160E-006                  7.2698748944159613E-094                  5.0179160420602321E-005                  2.2876560599578056E-007                  1.0000000000000004E-099                  1.0000000000000004E-099                  4.6668013273267904E-004                  2.0414644161309340E-007                  1.1381848144163240E-006                  1.0000000000000004E-099                  8.4443007282823002E-074                  2.0874146844472544E-008                  1.0000000000000004E-099                  1.0000000000000004E-099                  1.0269030813453577E-004                  6.7091975862945011E-006                  2.4972413232265062E-057                  2.0006886383171628E-004                  4.9786898247189956E+003                  2.1461143575428008E+002                  0.0000000000000000E+000                  4.5157989697983539E+004                  2.3297919714421755E+003                  1.0577389962355879E+001                  0.0000000000000000E+000                  0.0000000000000000E+000 
                                     592                  2.7239085937692584E+004                  8.0999231379148418E-001                  9.2768956348480064E-002                  0.0000000000000000E+000                  0.0000000000000000E+000                  1.1367933285512455E-001                  7.8129078429078902E-008                  1.0000000000000000E+000                  9.9317986516242851E-001                  1.1581426267158637E-001                  9.6386337090931164E-004                  6.9870341901610078E-001                  5.6962931836955688E-001                  0.0000000000000000E+000                 -1.9986631841832908E-001                 -4.8201405210184461E+001                  3.7897302509749204E+000                  6.3239593236503233E-001                 -1.9901093215713114E-001                 -1.5570713157577978E-001                  4.6577845143398982E+000                 -3.0000000000000475E+000                  7.1159420949650363E+000                  1.9980467324434472E+000                  1.7261751021496853E+001                  5.9749064052195178E-001                 -1.3921821623723432E+000                  7.4862898953216450E-001                  6.4224972602860048E-018                  1.1034185970759304E-004                  2.5025912131718681E-001                  3.6281941590277494E-016                  7.1065810408343176E-013                  4.6338906148233168E-023                  1.6086802751266569E-004                  1.9568213315500052E-006                  3.4867545924503404E-016                  6.0987289529204137E-005                  2.2388535189517994E-007                  2.9182327486870975E-031                  8.7407027840248354E-020                  4.6667973915111283E-004                  2.0414924054833912E-007                  1.1381343663647126E-006                  2.2855131115230559E-021                  9.1781576961675115E-023                  2.0874193793734392E-008                  3.7065645159884602E-042                  3.9314864173782023E-040                  1.0269030813439529E-004                  6.7091975862944926E-006                  1.5430912131334866E-016                  2.0006886383171560E-004                  7.4899999942868323E-001                  3.6108155599347527E-017                  2.6605602792408040E-005                  2.4997339496852472E-001                  6.1203516138300124E-037                  1.7349513354145605E-040                  1.0000000000000993E-099                  1.7017919585850678E-004                  1.9011704267928163E-006                  7.2796725886830642E-094                  5.0179160420602334E-005                  2.2876560599578053E-007                  1.0000000000000006E-099                  1.0000000000000006E-099                  4.6668013273267893E-004                  2.0414644161309337E-007                  1.1381848144163238E-006                  1.0000000000000006E-099                  8.4421321164489613E-074                  2.0874146844472544E-008                  1.0000000000000006E-099                  1.0000000000000006E-099                  1.0269030813453577E-004                  6.7091975862945019E-006                  4.1106152142620843E-057                  2.0006886383171622E-004                  4.9801556111713053E+003                  2.1465559040371008E+002                  0.0000000000000000E+000                  4.5171441672397355E+004                  2.3293127332935192E+003                  1.0560312314089561E+001                  0.0000000000000000E+000                  0.0000000000000000E+000 
                                     593                  4.0170168492729063E+004                  8.0999231238068270E-001                  9.2657535069124067E-002                  0.0000000000000000E+000                  0.0000000000000000E+000                  1.1367933305312516E-001                  7.8129078651123507E-008                  1.0000000000000000E+000                  9.9317381688473827E-001                  1.1581583973990918E-001                  9.6387699231684568E-004                  6.9858951468809860E-001                  5.6957730101692139E-001                  0.0000000000000000E+000                 -1.9996575097436850E-001                 -4.8201556735701352E+001                  3.7897085953642202E+000                  6.3206367855156909E-001                 -1.9923916562517330E-001                 -1.5577793708840007E-001                  4.6579261246087080E+000                 -3.0000000000000475E+000                  7.1159398087311061E+000                  1.9980283205803717E+000                  1.7261729896286983E+001                  5.9749089537331979E-001                 -1.3922185550828545E+000                  7.4862840591704072E-001                  6.4225121615288567E-018                  1.1029964301003699E-004                  2.5025974536316059E-001                  3.6268773618728560E-016                  7.1035719815415304E-013                  4.6313771343818876E-023                  1.6084390390354043E-004                  1.9837641461756074E-006                  3.4857298043539306E-016                  6.0988637058530873E-005                  2.2152584991130176E-007                  2.9169145056016640E-031                  8.7395177948976034E-020                  4.6667974035628692E-004                  2.0415056914861469E-007                  1.1381104254089161E-006                  2.2851269496277786E-021                  9.1769014090128329E-023                  2.0874216075586247E-008                  3.7053063551717499E-042                  3.9301281231132292E-040                  1.0269030813432880E-004                  6.7091975862944951E-006                  2.2754168226061389E-016                  2.0006886383171563E-004                  7.4899999942868323E-001                  3.6446705264203970E-017                  2.6605602792683519E-005                  2.4997339496852472E-001                  8.9903274053503869E-037                  1.7196986704942170E-040                  1.0000000000001490E-099                  1.7017919585850689E-004                  1.9011704267928175E-006                  7.2888696099299523E-094                  5.0179160420602334E-005                  2.2876560599578053E-007                  1.0000000000000004E-099                  1.0000000000000004E-099                  4.6668013273267893E-004                  2.0414644161309345E-007                  1.1381848144163238E-006                  1.0000000000000004E-099                  8.4345403543658127E-074                  2.0874146844472561E-008                  1.0000000000000004E-099                  1.0000000000000004E-099                  1.0269030813453578E-004                  6.7091975862945019E-006                  6.0248713184901274E-057                  2.0006886383171625E-004                  4.9819039607629957E+003                  2.1470824703177900E+002                  0.0000000000000000E+000                  4.5187487085895678E+004                  2.3287414755242025E+003                  1.0539230280853543E+001                  0.0000000000000000E+000                  0.0000000000000000E+000 

下面是我的代码的样子:

import matplotlib.pyplot as plt
import mesa_reader as mr
import pandas as pd

# load and plot data
#h = mr.MesaData('history.data')
h = pd.read_csv('history.data', header=4)
#plt.plot(10**h.log_Teff, h.luminosity)
#plt.plot(10**h["log_Teff"], h["luminosity"])
#print("The column headers :") 
#print(list(h.columns.values)) 
print(h['model_number'])

以及错误:

Traceback (most recent call last):
  File "/home/nick/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2898, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'model_number'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "HR_plotter.py", line 12, in <module>
    print(h['model_number'])
  File "/home/nick/.local/lib/python3.6/site-packages/pandas/core/frame.py", line 2906, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/nick/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2900, in get_loc
    raise KeyError(key) from err
KeyError: 'model_number'


Tags: incorelognumberpandasgetmodelline