将函数应用于 Pandas Python 中的每一行时发生数据转换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39214164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data Conversion Error while applying a function to each row in pandas Python
提问by dragster
I have a data frame in pandas in python which resembles something like this -
我在 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
Now I want to apply a function to each row of this dataframe The function is written as this -
现在我想对这个数据帧的每一行应用一个函数函数是这样写的 -
def findCluster(clusterModel,data):
return clusterModel.predict(data)
I apply this function to each row in this manner -
我以这种方式将此函数应用于每一行 -
df_fil.apply(lambda x : findCluster(cluster_all,x.reshape(1,-1)),axis=1)
When I run this code, I get a warning saying -
当我运行此代码时,我收到一条警告说 -
DataConversionWarning: Data with input dtype object was converted to float64.
warnings.warn(msg, DataConversionWarning)
DataConversionWarning:具有输入 dtype 对象的数据已转换为 float64。
警告。警告(味精,数据转换警告)
This warning is printed once for each row. Since, I have around 450K rows in my data frame, my computer hangs while printing all these warning messages that too on ipython notebook.
该警告每行打印一次。因为,我的数据框中有大约 450K 行,所以我的计算机在 ipython notebook 上打印所有这些警告消息时挂起。
But to test my function I created a dummy dataframe and tried applying the same function on that and it works well. Here is the code for that -
但是为了测试我的功能,我创建了一个虚拟数据框并尝试对其应用相同的功能,并且效果很好。这是代码 -
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)
The output to this is -
对此的输出是 -
0 1 2
0 4 4 4
1 4 4 4
Can anyone suggest what am I doing wrong or what can I change to make this error go away?
任何人都可以建议我做错了什么,或者我可以改变什么来消除这个错误?