【发布时间】:2019-05-11 06:53:43
【问题描述】:
所以我有一个 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
我假设最后一行会引发该错误,因为df[df.foo == 222]['foo'] 是Series:
2 222
Name: foo, dtype: int64
所以我试图获得价值本身。我用.at 得到了这个:
print(df[df.foo == 222].loc[:,'bar'].at['bar'])
#ValueError: At based indexing on an integer index can only have integer indexers
据我所知,iat 使用整数索引器,at 同时使用标签和整数,那么这里发生了什么?
【问题讨论】:
标签: python python-3.x pandas dataframe