如何在python中对数据帧的每一行进行计算?
我是python的新手,我正在尝试将我在R中创建的函数转换为python,R函数如下所述: 从我的阅读来看,在python中实现这一点的最佳方法是使用for循环,其形式如下如何在python中对数据帧的每一行进行计算?,python,pandas,for-loop,Python,Pandas,For Loop,我是python的新手,我正在尝试将我在R中创建的函数转换为python,R函数如下所述: 从我的阅读来看,在python中实现这一点的最佳方法是使用for循环,其形式如下 for line 1 in probe test find user in U_lookup find movie in M_lookup take the value found in U_lookup and retrieve that line number from knn_text take the va
for line 1 in probe test
find user in U_lookup
find movie in M_lookup
take the value found in U_lookup and retrieve that line number from knn_text
take the values found in that row of knn_text, and retrieve the line numbers from dfm
for those line numbers in dfm, retrieve column=U_lookup
take the average of the non zero values found
save value into pandas datafame in new column for that line
这是完成这样一个操作的最有效的方法吗?来自R,所以我不确定pandas包中是否有更好的功能
接下来,python中是否有与R中的函数dput()等效的函数?dput本质上提供了代码,可以方便地共享此类问题的数据子集。您可以使用df.apply(my_func,axis=1)
将函数/计算应用于数据帧的每一行。
其中,my_func将包含所需的计算首先搜索您的问题,循环几乎肯定不是最有效的方法。您可能想要
pandas.merge
或只是一个简单的映射,但是如果您想要一个详细的答案,您需要创建一个映射。