python-3.x - 将函数应用于具有向量返回轴相关错误的数据框?
问题描述
我有以下函数、数据框和向量,为什么会出现错误?
import pandas as pd
import numpy as np
def vanilla_vec_similarity(x, y):
x.drop('request_id', axis=1, inplace=True).values.flatten().tolist()
y.drop('request_id', axis=1, inplace=True).values.flatten().tolist()
res = (np.array(x) == np.array(y)).astype(int)
return res.mean()
test_df = pd.DataFrame({'request_id': [55, 42, 13], 'a': ['x','y','z'], 'b':[1,2,3], 'c': [1.0, -1.8, 19.113]})
test_vec = pd.DataFrame([[123,'x',1.1, -1.8]], columns=['request_id', 'a', 'b', 'c'])
test_df['similarity'] = test_df.apply(lambda x: vanilla_vec_similarity(x, test_vec), axis=1)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in _get_axis_number(cls, axis)
367 try:
--> 368 return cls._AXIS_TO_AXIS_NUMBER[axis]
369 except KeyError:
KeyError: 1
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
10 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in _get_axis_number(cls, axis)
368 return cls._AXIS_TO_AXIS_NUMBER[axis]
369 except KeyError:
--> 370 raise ValueError(f"No axis named {axis} for object type {cls.__name__}")
371
372 @classmethod
ValueError: No axis named 1 for object type Series
解决方案
您可以通过以下更改使此代码工作:
def vanilla_vec_similarity(x, y):
x.drop('request_id', axis=1).values.flatten().tolist()
y.drop('request_id', axis=1).values.flatten().tolist()
res = (np.array(x) == np.array(y)).astype(int)
return res.mean()
test_df = pd.DataFrame({'request_id': [55, 42, 13], 'a': ['x','y','z'], 'b':[1,2,3], 'c': [1.0, -1.8, 19.113]})
test_vec = pd.DataFrame([[123,'x',1.1, -1.8]], columns=['request_id', 'a', 'b', 'c'])
test_df['similarity'] = test_df.apply(lambda x: vanilla_vec_similarity(x.to_frame().T, test_vec), axis=1)
解释:
- 首先,当你这样做时,你
test_df.apply(lambda x: vanilla_vec_similarity(x, test_vec), axis=1)
将每一行作为一个系列(列名作为系列的索引)传递给函数。 - 代码中断是因为您试图删除列
request_id
,因为它不存在。 - 你也不需要使用
inplace=True.
或者您可以使用:
test_df['similarity'] = test_df.apply(lambda x: x[1:].eq(pd.Series(test_vec.loc[0])[1:]).mean(), axis=1)
或者,如果您定义test_vec
为Series
而不是Dataframe
:
test_vec = pd.Series([123,'x',1.1, -1.8], index=['request_id', 'a', 'b', 'c'])
test_df['similarity'] = test_df.apply(lambda x: x[1:].eq(test_vec[1:]).mean(), axis=1)
推荐阅读
- python - Flask:如何根据按钮输入更新sqlite上的信息
- node.js - 如何在我的 cocod2d-x (C++) 游戏中集成 Node.js 服务器
- python - 关于添加“数组”元素的Python问题
- package - 安装不平衡学习的环境问题
- ssl - 为多个 Traefik 服务共享 Acme 配置
- linux - 使用 Makefile 遍历目录
- sqlalchemy - SQLAlchemy - 如何使每个 ForeignKey 关系的列唯一?
- tomcat - 通过 context.xml 禁用一些 http 方法
- java - WildFly 中的 RestEasy UTF-8 响应
- automotive - 如何在 AGL 中显示弹出窗口或警报?