将列表传递到Loc

问题描述

我的数据框如下:

tuples = [
   ('cobra','mark i'),('cobra','mark ii'),('sidewinder',('viper','mark iii')
]
index = pd.MultiIndex.from_tuples(tuples)
values = [[12,2],[0,4],[10,20],[1,[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循环加载变量,但这也不起作用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)