简体   繁体   中英

Can't figure out why I keep getting a KeyError while trying to the number of a CSV row

The code is super simple and the content of the cell is the exact same as what I'm writing into the code. Just trying to get the row number for all of the times where the ticker column = A.

Code:

import pandas as pd

filename = 'DataFiles/SHARADAR_SF1_aafe962511a67db10c0a72fe536305b0.csv'
pattern = 'AAPL'

df = pd.read_csv(filename, index_col=0)

rows = df[df['ticker'] == pattern].index.to_list()

Example of the CSV (There are more tickers later in the file, for example AAPL or TSLA etc.):

ticker,dimension,calendardate,datekey,lastupdated,assets,assetsavg,cashneq,debt,debtc,debtusd,divyield,deposits,eps,epsusd,equity,equityavg,liabilities,netinc,pe,price,revenue
A,ARQ,1999-12-31,2000-03-15,2020-09-01,7107000000,,1368000000,665000000,111000000,665000000,0,0,0.3,0.3,4486000000,,2621000000,131000000,,114.3,2246000000
A,ARQ,2000-03-31,2000-06-12,2020-09-01,7321000000,,978000000,98000000,98000000,98000000,0,0,0.37,0.37,4642000000,,2679000000,166000000,,66,2485000000
A,ARQ,2000-06-30,2000-09-01,2020-09-01,7827000000,,703000000,129000000,129000000,129000000,0,0,0.34,0.34,4902000000,,2925000000,155000000,46.877,61.88,2670000000
A,ARQ,2000-09-30,2001-01-17,2020-09-01,8425000000,,996000000,110000000,110000000,110000000,0,0,0.67,0.67,5265000000,,3160000000,305000000,37.341,61.94,3372000000
A,ARQ,2000-12-31,2001-03-19,2020-09-01,9208000000,,433000000,556000000,556000000,556000000,0,0,0.34,0.34,5541000000,,3667000000,154000000,21.661,36.99,2841000000

Here, use index_col as None, otherwise ticker is index column:

import pandas as pd

filename = 'DataFiles/SHARADAR_SF1_aafe962511a67db10c0a72fe536305b0.csv'
pattern = 'AAPL'

df = pd.read_csv(filename, index_col=None)

rows = df[df['ticker'] == pattern].index.to_list()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM