我在 python 的 pandas 中有一个数据框,类似于这样的东西 -
contest_login_count contest_participation_count ipn_ratio
0 1 1 0.000000
1 3 3 0.083333
2 3 3 0.000000
3 3 3 0.066667
4 5 13 0.102804
5 2 3 0.407407
6 1 3 0.000000
7 1 2 0.000000
8 53 91 0.264151
9 1 2 0.000000
现在我想对该数据帧的每一行应用一个函数该函数写成这样 -
def findCluster(clusterModel,data):
return clusterModel.predict(data)
我以这种方式将此函数应用于每一行 -
df_fil.apply(lambda x : findCluster(cluster_all,x.reshape(1,-1)),axis=1)
当我运行此代码时,我收到一条警告:
DataConversionWarning:具有输入 dtype 对象的数据已转换为 float64。
warnings.warn(msg, DataConversionWarning)
该警告每行打印一次。因为我的数据框中有大约 450K 行,所以我的计算机在 ipython 笔记本上打印所有这些警告消息时挂起。
但为了测试我的函数,我创建了一个虚拟数据框,并尝试对其应用相同的函数,效果很好。这是代码 -
t = pd.DataFrame([[10.35,100.93,0.15],[10.35,100.93,0.15]])
t.apply(lambda x:findCluster(cluster_all,x.reshape(1,-1)),axis=1)
其输出是 -
0 1 2
0 4 4 4
1 4 4 4
谁能建议我做错了什么或者我可以改变什么来消除这个错误?