简体   繁体   English

Python Pandas Dataframe.at function

[英]Python Pandas Dataframe.at function

using使用

a = df.at('x', 'y')

should this return the value of the cell in the row containing 'x' and the column header 'y'?这应该返回包含“x”的行中单元格的值和列 header“y”吗?

should it matter that the row containing 'x' is not either the name of the row or in the first column?包含“x”的行既不是行名也不是第一列,这有关系吗?

If it doesn't matter can anyone suggest what might be the problem here.如果没关系,任何人都可以建议这里可能是什么问题。

Here is some sample data这是一些示例数据

     Account   Asset Symbol SecType Currency  Position  Avg cost
0  DU2870371  USDCAD    USD    CASH      CAD -232475.0  1.286397
2  DU2870371  AUDUSD    AUD    CASH      USD  285611.0  0.756396

I have then saved this to a csv. In another script I have loaded it from CSV and using然后我将其保存到 csv。在另一个脚本中,我从 CSV 加载它并使用

a = df.at['USDCAD', 'Position']

I would expect it to return the value -232475.0 but instead is says KeyError: 'USDCAD'我希望它返回值-232475.0而是说 KeyError: 'USDCAD'

Connvert column Asset to index for select by labels in index (from Asset column) and columns names:通过索引中的标签(来自资产列)和列名称将列Asset转换为 select 的索引:

df = df.set_index('Asset')
a = df.at['USDCAD', 'Position']

Also is possible use boolean indexing , but output is empty, one or more values in Series :也可以使用boolean indexing ,但是 output 是空的,一个或多个值在Series中:

s = df.loc[df['Asset'].eq('USDCAD'), 'Position']

So for first value of Series is possible use:因此,对于 Series 的第一个值,可以使用:

a = s.iat[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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