dra*_*ter 8 python function apply dataframe pandas
我在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
Run Code Online (Sandbox Code Playgroud)
现在我想将一个函数应用于此数据帧的每一行该函数被写为 -
def findCluster(clusterModel,data):
return clusterModel.predict(data)
Run Code Online (Sandbox Code Playgroud)
我以这种方式将此函数应用于每一行 -
df_fil.apply(lambda x : findCluster(cluster_all,x.reshape(1,-1)),axis=1)
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我收到一条警告 -
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)
Run Code Online (Sandbox Code Playgroud)
这个输出是 -
0 1 2
0 4 4 4
1 4 4 4
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议我做错了什么或者我可以改变什么来使这个错误消失?
jez*_*ael 11
我觉得dtype
有些专栏有问题没有float
.
你需要把它投射astype
:
df['colname'] = df['colname'].astype(float)
Run Code Online (Sandbox Code Playgroud)