如何访问熊猫中数据框的特定元素。给出错误

发布于 2025-01-26 11:01:26 字数 2732 浏览 1 评论 0 原文

我有一个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'

I have a dataframe df_params. It contains parameters for the stored procedure.

   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

I simply want to access the elements of the dataframe in a loop:

for i in range(len(df_params)):
    print(df_params[i][0])

But it gives me an error without really explanation:

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>

The goal is to supply value to the stored procedure:

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)

desired outcome from 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贵在坚持 2025-02-02 11:01:26

pandas.dataframe s的行为不完全像 numpy.ndarray s。基本上有三个选项:

选项1:iterrows-hethod:
您可以迭代pandas.dataframe的行,

for idx, row in df_params.iterrows():
    print(row['PurchaseOrderID'])

这是一种特别可读的方法,因此我个人更喜欢此

选项2:索引:
if you want to index pandas.dataframe just like an numpy.ndarray object, go with the method

for i in range(len(df_params)):
    print(df_params.iat[i, 0])

​不同的索引(在极端的某些字符串或带有 pandas.datatimeindex >的表格)仍然有效...好像您将完成 df_params.to_numpy()[i,i, 0]
注意:存在类似的函数,该功能使用列名:

​关于列: 采用 index 和列名称

for idx in df_params.index:
    print(df_params.iloc[idx, 'PurchaseOrderID'])

选项3:切片pandas.series对象:
pandas.dataframe 中的每一列都是 pandas.Series 对象,您可以相似地索引它(实际上,您 index series 如上所述)到 numpy.ndarray

col = df_params['PurchaseOrderID']
for idx in col.index:
    print(col[idx])

那么,您的情况出了什么问题?
双重索引几乎与上一个示例它呼叫 .iloc [] 。
因此,如果您真的愿意,您可以这样去:

for i in range(len(df_params)):
    print(df_params.iloc[0][i])

,但这仅是因为 pandas.dataframe 具有从0!开始的连续数字索引因此,请不要执行此操作,并使用表的实际索引(实际上使用上面的选项之一,而不是最后一个选项;)

pandas.DataFrames don't behave exactly like numpy.ndarrays. There are basically three options:

option 1: iterrows-method:
You can iterate over rows of a pandas.dataframe by

for idx, row in df_params.iterrows():
    print(row['PurchaseOrderID'])

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[]

for i in range(len(df_params)):
    print(df_params.iat[i, 0])

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 a df_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)

for idx in df_params.index:
    print(df_params.iloc[idx, 'PurchaseOrderID'])

option 3: slicing a pandas.Series object:
Every column in a pandas.DataFrame is a pandas.Series object, which you can index similar (you actually index the series as described above) to a numpy.ndarray:

col = df_params['PurchaseOrderID']
for idx in col.index:
    print(col[idx])

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:

for i in range(len(df_params)):
    print(df_params.iloc[0][i])

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 ;) )

不…忘初心 2025-02-02 11:01:26

在数据框架上,有更好的方法访问值,您可以使用lambda。
使用Lambda可以访问任何行。

df.apply(lambda row : print(row['DF_Name']))

现在,变量“行”是数据框架上的每一行,您可以访问行上的每个属性。

On data frame there are better ways to access values, you can use lambda.
with lambda will have an access to any row.

df.apply(lambda row : print(row['DF_Name']))

now the variable 'row' is each row on the dataframe, and you can access to each properties on the row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文