pandas apply typeError: 'float' object is not subscriptable
pandas apply typeError: 'float' object is not subscriptable
我有一个这样的数据框 df_tr
:
item_id target target_sum target_count
0 0 0 1 50
1 0 0 1 50
我试图找到目标的平均值但不包括当前行的目标值,并将平均值放在新列中。结果将是:
item_id target target_sum target_count item_id_mean_target
0 0 0 1 50 0.02041
1 0 0 1 50 0.02041
我从公式中计算出 item_id_mean_target
值:
target_sum - target/target_count - 1
...使用此代码:
df_tr['item_id_mean_target'] = df_tr.target.apply(lambda x: (x['target_sum']-x)/(x['target_count']-1))
我认为我的解决方案是正确的,但我却得到了:
TypeError: 'float' object is not subscriptable
试试这个:
df_tr.apply(lambda x:(x['target_sum']-x)/(x['target_count']-1), axis=1)
这里不需要申请,pandas(因此是 numpy)广播操作。
df['item_id_mean_target'] = (df.target_sum - df.target) / (df.target_count - 1)
df
item_id target target_sum target_count item_id_mean_target
0 0 0 1 50 0.020408
1 0 0 1 50 0.020408
至于为什么会出现错误,您正在 pd.Series
对象上调用 apply
,因此,您不能引用 apply
中的任何其他列(因为它只接收标量值)。
要修复它,您需要执行 df.apply(...)
但到那时,您会因性能低下而受到惩罚,因此,我不建议您这样做。
忽略 sum 和 count 列并使用 groupby
推导它们:
df_tr.groupby('item_id').apply(lambda x: (x['target'].sum() - x['target'])
/ (x['target'].count() - 1))
您可能还会注意到您的原始陈述中的问题 x['target_sum']-x
。应该是 x['target_sum']-x['target']
.
我有一个这样的数据框 df_tr
:
item_id target target_sum target_count
0 0 0 1 50
1 0 0 1 50
我试图找到目标的平均值但不包括当前行的目标值,并将平均值放在新列中。结果将是:
item_id target target_sum target_count item_id_mean_target
0 0 0 1 50 0.02041
1 0 0 1 50 0.02041
我从公式中计算出 item_id_mean_target
值:
target_sum - target/target_count - 1
...使用此代码:
df_tr['item_id_mean_target'] = df_tr.target.apply(lambda x: (x['target_sum']-x)/(x['target_count']-1))
我认为我的解决方案是正确的,但我却得到了:
TypeError: 'float' object is not subscriptable
试试这个:
df_tr.apply(lambda x:(x['target_sum']-x)/(x['target_count']-1), axis=1)
这里不需要申请,pandas(因此是 numpy)广播操作。
df['item_id_mean_target'] = (df.target_sum - df.target) / (df.target_count - 1)
df
item_id target target_sum target_count item_id_mean_target
0 0 0 1 50 0.020408
1 0 0 1 50 0.020408
至于为什么会出现错误,您正在 pd.Series
对象上调用 apply
,因此,您不能引用 apply
中的任何其他列(因为它只接收标量值)。
要修复它,您需要执行 df.apply(...)
但到那时,您会因性能低下而受到惩罚,因此,我不建议您这样做。
忽略 sum 和 count 列并使用 groupby
推导它们:
df_tr.groupby('item_id').apply(lambda x: (x['target'].sum() - x['target'])
/ (x['target'].count() - 1))
您可能还会注意到您的原始陈述中的问题 x['target_sum']-x
。应该是 x['target_sum']-x['target']
.