Pandas .at 抛出 ValueError: At 基于整数索引的索引只能有整数索引器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53704552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:12:15  来源:igfitidea点击:

Pandas .at throwing ValueError: At based indexing on an integer index can only have integer indexers

pythonpython-3.xpandasdataframe

提问by Nathan Tew

So I have a df where I am extracting one value to store it in another df:

所以我有一个 df,我在其中提取一个值以将其存储在另一个 df 中:

import pandas as pd

# Create data set
d = {'foo':[100, 111, 222], 
     'bar':[333, 444, 555]}
df = pd.DataFrame(d)

# Full dataframe:
print(df)

# Shows:
#    bar   foo 
# 0  333   100
# 1  444   111
# 2  555   222

df2=pd.DataFrame()
df2.loc[1,'Name'] = df[df.foo == 222]['foo']

#error: 
ValueError: Incompatible indexer with Series

I'm assuming the last line throws that error because df[df.foo == 222]['foo']is a Series:

我假设最后一行抛出该错误,因为df[df.foo == 222]['foo']是一个Series

2    222
Name: foo, dtype: int64

So I'm trying to get the value itself. I used .atand got this:

所以我试图获得价值本身。我使用.at并得到了这个:

print(df[df.foo == 222].loc[:,'bar'].at['bar'])

#ValueError: At based indexing on an integer index can only have integer indexers

From what I've read it's iatthat uses integer indexers and atuses both label and integer, so what's going on here?

从我读过的内容来看iat,它使用整数索引器并同时at使用标签和整数,那么这里发生了什么?

回答by cs95

Using atwith a boolean mask is considered bad form unless you can 100% guarantee only one row in the mask is true (otherwise, atfails).

使用at用布尔面具被认为是不好的形式,除非你能保证100%的面具只有一行是真实的(否则,at失败)。

The best thing to do is to use locand take the first result.

最好的办法是使用loc并取第一个结果。

df.loc[df.foo == 222, 'bar'].values[0]
555

For reference, atdoes not work because returns a single-row Series with a index [2]:

作为参考,at不起作用,因为返回带有索引的单行系列[2]

df[df.foo == 222].loc[:,'bar']

2    555
Name: bar, dtype: int64

At this point, at['bar']makes no sense because it searches for "bar" in the index and barisn't. What you should've done is

在这一点上,at['bar']没有意义,因为它在索引中搜索“bar”而bar不是。你应该做的是

df[df.foo == 222].at[2, 'bar']
555

回答by meW

You can easily get the value using values

您可以使用 values

df2.loc[1,'Name'] = df[df.foo == 222]['foo'].values
df2

#   Name
# 1 222

回答by Vasu Bandaru

The point at which you're using at, the data is a Pandas Series with integer Index, That is why your getting the mentioned error.

您使用at的数据是带有整数索引的 Pandas 系列,这就是您收到上述错误的原因。

#ValueError: At based indexing on an integer index can only have integer indexers

If you check the index of the data, you'll see an index with value 2

如果你检查数据的索引,你会看到一个值为 2 的索引

df[df.foo == 222].loc[:,'bar'].index

#Int64Index([2], dtype='int64')

One of the correct method would be, as mentioned by coldspeed

正如coldspeed所提到的,正确的方法之一是

df.loc[df.foo == 222].at[2,'bar']

#555

回答by Shifu

.atwill work with label indexing, not position indexing.
Example:

.at将使用标签索引,而不是位置索引。
例子:

df.at(3,'ColName')

Returns value of ColNamefor 3rd row.

返回ColName第三行的值。