2024-04-25 04:43:22 发布
网友
在pandas数据帧中使用Lambda时,如何正确引用另一列值。在
dfresult_tmp2['Retention_Rolling_temp'] = dfresult_tmp2['Retention_tmp'].apply(lambda x: x if x['Count Billings']/4 < 0.20 else '')
上面的代码给出了这个错误。在
dfresult_tmp2['Retention_tmp'].apply( lambda x: x if x['Count Billings'] / 4 < 0.20 else '' )
您使用的是与pd.DataFrame.apply不同的pd.Series.apply。在本例中,您将迭代地向lambda传递一个标量值。所以some_scalar_x['Count Billings']没有意义。在
pd.DataFrame.apply
pd.Series.apply
some_scalar_x['Count Billings']
我将向您展示矢量化版本,而不是告诉您如何将逻辑压缩成apply
apply
选项1pd.Series.where
pd.Series.where
dfresult_tmp2['Retention_tmp'] = \ dfresult_tmp2['Retention_tmp'].where( dfresult_tmp2['Count Billings'] / 4 < .2, '')
选项2np.where
np.where
选项3apply 你要的不是我推荐的。在
dfresult_tmp2['Retention_tmp'] = dfresult_tmp2.apply( lambda x: x['Retention_tmp'] if x['Count Billings'] / 4 < .2 else '' )
您使用的是与
pd.DataFrame.apply
不同的pd.Series.apply
。在本例中,您将迭代地向lambda传递一个标量值。所以some_scalar_x['Count Billings']
没有意义。在我将向您展示矢量化版本,而不是告诉您如何将逻辑压缩成
apply
选项1
pd.Series.where
选项2
^{pr2}$np.where
选项3
apply
你要的不是我推荐的。在
相关问题 更多 >
编程相关推荐