如何访问熊猫中数据框的特定元素。给出错误
我有一个dataframe df_params
。它包含存储过程的参数。
PurchaseOrderID OrderDate SupplierReference DF_Name
0 1 2013-01-01 B2084020 dataframe1
1 2 2013-01-01 293092 dataframe2
2 3 2013-01-01 08803922 dataframe3
3 4 2013-01-01 BC0280982 dataframe4
4 5 2013-01-01 ML0300202 dataframe5
我只想在循环中访问数据框的元素:
for i in range(len(df_params)):
print(df_params[i][0])
但是它给我一个错误而没有真正的解释:
Traceback (most recent call last):
File "C:my\path\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Test3.py", line 35, in <module>
print(df_params[i][0])
File "C:\Users\my\path\Python37\lib\site-packages\pandas\core\frame.py", line 2995, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\my\path\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
PS Microsoft.PowerShell.Core\FileSystem::\\my\path>
目标是为存储过程提供价值:
for i in range(len(df_params)):
query = "EXEC Purchasing.GetPurchaseOrder " + df_params[i][0] + "," + str(df_params[i][1]) + "," + df_params[i][2])
df = pd.read_sql(query, conn)
print(QUERY)所需的结果::
EXEC Purchasing.GetPurchaseOrder 1, '2013-01-01', 'B2084020'
EXEC Purchasing.GetPurchaseOrder 2, '2013-01-01', '293092'
EXEC Purchasing.GetPurchaseOrder 3, '2013-01-01', '08803922'
EXEC Purchasing.GetPurchaseOrder 4, '2013-01-01', 'BC0280982'
EXEC Purchasing.GetPurchaseOrder 5, '2013-01-01', 'ML0300202'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pandas.dataframe
s的行为不完全像numpy.ndarray
s。基本上有三个选项:选项1:iterrows-hethod:
您可以迭代pandas.dataframe的行,
这是一种特别可读的方法,因此我个人更喜欢此
选项2:索引:
if you want to index pandas.dataframe just like an numpy.ndarray object, go with the method
不同的索引(在极端的某些字符串或带有
pandas.datatimeindex
>的表格)仍然有效...好像您将完成df_params.to_numpy()[i,i, 0]
。注意:存在类似的函数,该功能使用列名:
关于列: 采用 index 和列名称
选项3:切片pandas.series对象:
pandas.dataframe
中的每一列都是pandas.Series
对象,您可以相似地索引它(实际上,您 index series 如上所述)到numpy.ndarray
:那么,您的情况出了什么问题?
双重索引几乎与上一个示例但它呼叫 .iloc [] 。
因此,如果您真的愿意,您可以这样去:
,但这仅是因为
pandas.dataframe
具有从0!开始的连续数字索引因此,请不要执行此操作,并使用表的实际索引(实际上使用上面的选项之一,而不是最后一个选项;)pandas.DataFrame
s don't behave exactly likenumpy.ndarray
s. There are basically three options:option 1: iterrows-method:
You can iterate over rows of a pandas.dataframe by
This is a particularly readable way, so personally I prefer this
option 2: indexing:
if you want to index pandas.dataframe just like an numpy.ndarray object, go with the method
.iat[]
This actually indexes all elements and ignores the index of the dataframe! So assuming that you have a different index (in the extreme some strings or a table with a
pandas.DataTimeIndex
) this still works... just as if you would have done adf_params.to_numpy()[i, 0]
.Note: There exists a similar function that uses the column name:
.at[]
There is a second way to index a
pandas.DataFrame
object and it is just a little safer with regard to columns:.loc[]
It takes an index and column name(s)option 3: slicing a pandas.Series object:
Every column in a
pandas.DataFrame
is apandas.Series
object, which you can index similar (you actually index the series as described above) to anumpy.ndarray
:So what went wrong in your case?
The double indexing is almost the same as the last example but it calls calls
.loc[]
under the hood and thus expects a column name and not a number (that would have been the method.iloc[]
. And it is expecting to see the column first and then the row.So if you really want, you could go like this:
but this only works because your
pandas.DataFrame
has continuous numeric indices starting from 0! So please don't do this and use the actual indices of your table (actually use one of the options above and not the last one ;) )在数据框架上,有更好的方法访问值,您可以使用lambda。
使用Lambda可以访问任何行。
现在,变量“行”是数据框架上的每一行,您可以访问行上的每个属性。
On data frame there are better ways to access values, you can use lambda.
with lambda will have an access to any row.
now the variable 'row' is each row on the dataframe, and you can access to each properties on the row.