python - 如何通过定义行的第一个单元格中的文本来获取 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
解决方案
使用loc
而不是iloc
, 因为iloc
适用于索引值(整数),其中 asloc
可以使用列名,还请确保您的数据库中有一个名为 的索引Apple
。
推荐阅读
- botframework - 如何将 JSON 值绑定到 Bot composer lg 中的活动建议操作
- php -
(正则表达式)当两个出现在字符串 PHP 中时替换为空 - python - ValueError:对于输入形状为 [?,1,1,128] 的 MaxPool1D,从 1 中减去 2 导致的负尺寸大小。帖子中的完整代码、输出和错误:
- javascript - 在nodejs中获取html数据(电子gui)
- sql-server - 关于将数据从 Azure SQL 服务器数据库传输到 Snowflake 的建议
- reactjs - React:File-Saver 在文件名之间添加下划线
- javascript - Mermaid FlowChart 在 Vue 3 中无法使用动态数据正确渲染
- postgresql - 来自分区表的 postgres 错误估计
- angular - *44 open() "/usr/share/nginx/html/att-app" failed (2: No such file or directory), 404 not Found
- android - 强制 gradle 不升级传递依赖