我有一个这样的数据集
a_id b_received brand_id c_consumed type_received date output \
0 sam soap bill oil edibles 2011-01-01 1
1 sam oil chris NaN utility 2011-01-02 1
2 sam brush dan soap grocery 2011-01-03 0
3 harry oil sam shoes clothing 2011-01-04 1
4 harry shoes bill oil edibles 2011-01-05 1
5 alice beer sam eggs breakfast 2011-01-06 0
6 alice brush chris brush cleaning 2011-01-07 1
7 alice eggs NaN NaN edibles 2011-01-08 1
我正在使用以下代码
def probability(x):
y=[]
for i in range(len(x)):
y.append(float(x[i])/float(len(x)))
return y
df2['prob']= (df2.groupby('a_id')
.apply(probability(['output']))
.reset_index(level='a_id', drop=True))
理想的结果应该是具有以下值的新列
prob
0 0.333334
1 0.333334
2 0.0
3 0.5
4 0.5
5 0
6 0.333334
7 0.333334
但我犯了个错误
y.append(float(x[i])/float(len(x)))
ValueError: could not convert string to float: output
列输出为int格式。我不明白为什么我会犯这个错误。你知道吗
我试图计算每个人消费产品的概率,这个概率由列output给出。例如,如果sam接收到soap,并且soap也出现在列“c\u consumered”中,则结果为1,否则结果为0。你知道吗
现在,因为萨姆收到了3个产品,他消耗了其中的2个,所以每个产品消耗的概率是1/3。所以输出为1的概率应该是0.333334,输出为0的概率应该是0。你知道吗
如何达到预期的效果?你知道吗
我认为您可以简单地将
output
列与已计算的分组.groupby('a_id')['output']
一起传递给GroupBy
对象,然后使用函数probability
,该函数只返回带len
的除法列output
:或使用
lambda
:使用^{} 可以实现更简单、更快的解决方案:
时间安排:
相关问题 更多 >
编程相关推荐