首页 > 解决方案 > 如何通过定义行的第一个单元格中的文本来获取 CSV 行中的所有数据?

问题描述

我试图通过在 iloc 中使用“Apple”来做到这一点,但它给了我一个回溯。我知道在使用 iloc 时,[] 中的任何内容都必须是整数,所以我如何找到类似单元格的“Apple”

file1 = pd.read_csv('SHARADAR_SF1_aafe962511a67db10c0a72fe536305b0.csv', usecols=['ticker','datekey','assets','eps','pe','price','revenue'])

print(file1.iloc['Apple'])

错误信息:

Traceback (most recent call last):
  File "C:/Users/George Adamopoulos/Desktop/All My Files/Neptune Financial Inc/The White Tiger JV/Research/20 Variables Research Code/DataReader.py", line 16, in <module>
    print(file1.iloc['Apple'])
  File "C:\Users\George Adamopoulos\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1767, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Users\George Adamopoulos\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2134, in _getitem_axis
    raise TypeError("Cannot index by location index with a non-integer key")
TypeError: Cannot index by location index with a non-integer key

CSV 的几行:

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

标签: pythonpandas

解决方案


使用loc而不是iloc, 因为iloc适用于索引值(整数),其中 asloc可以使用列名,还请确保您的数据库中有一个名为 的索引Apple


推荐阅读