0

I have a joint_tabel, after selecting partial table by using certain LOGIN_ID (I give the name 'lenders_table'), I can print the lenders_table well, but when I print the first row of lenders_table, I got the error: " raise KeyError(key) from err, KeyError: 0". I cannot find the reason. Thanks

lenders_table = joint_table.loc[joint_table['LOGIN_ID'] == lender_name]

print(lenders_table)

    LOAN_ID  ...  REPEAT_LOAN
12  351422   ...           No
14  350948   ...           No

[2 rows x 14 columns]

print(lenders_table.loc[0])

"""err here"""
swing
  • 139
  • 2
  • 5
  • 12
  • 3
    As you can see, the index of the first row is "12", not "0". `loc` expects you to pass in labels, not positions. try `lenders_table.iloc[0]` – cs95 Apr 11 '23 at 19:48
  • 1
    Alternatively, you can reset the index to again index your data starting from 0 (if you don't mind losing the original indexing) using the [reset_index() method](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html) Afterwards, even the label indexing via `loc` will behave in the manner you expect (first row has label `0`). – mcvincekova Apr 11 '23 at 19:52

0 Answers0