将列表传递到Loc

时间:2020-11-02 18:29:21

标签: python multi-index loc isin

我的数据框如下:

tuples = [
   ('cobra', 'mark i'), ('cobra', 'mark ii'),
   ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'),
   ('viper', 'mark ii'), ('viper', 'mark iii')
]
index = pd.MultiIndex.from_tuples(tuples)
values = [[12, 2], [0, 4], [10, 20],
        [1, 4], [7, 1], [16, 36]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)

index.name = 'name, type'

我正在尝试使用另一个df df2中的值来匹配对,并将“ shield”值返回到df2中,如下所示:

name = df2['names']
type = df2['type']
components = df2['components']

df2['shield'] = df.loc[(name,type),components]

但是当我尝试传递列表变量时收到以下错误...

KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'

是否可以通过列表找到多个索引?我还尝试通过三重for循环加载变量,但这也不起作用

0 个答案:

没有答案