我想在名为str_bos
的现有DataFrame
中创建一个名为result
的列。我有以下列 - 'str_nbr', 'ZIP Sales', 'str_Sales', 'ZIP_Distinct #', 'ZIP_Share_of_Str_Sales', 'Counter', 'Str_BOS_Cum%', 'Str_Sales_Rank'
。
这是我提出的。但是,它需要2个小时才能完成。但是,其他操作(如排序,合并等)需要几秒钟。我在这里失踪了什么?
def str_bos(row):
if row['str_sales_rank'] == 1 or row['str_bos_cum%'] <= 0.1:
return 1
elif row['str_bos_cum%'] <= 0.2:
return 2
elif row['str_bos_cum%'] <= 0.3:
return 3
elif row['str_bos_cum%'] <= 0.4:
return 4
elif row['str_bos_cum%'] <= 0.5:
return 5
elif row['str_bos_cum%'] <= 0.6:
return 6
elif row['str_bos_cum%'] <= 0.7:
return 7
elif row['str_bos_cum%'] <= 0.8:
return 8
elif row['str_bos_cum%'] <= 0.9:
return 9
else:
return 10
result['str_bos'] = result.apply(lambda row: str_bos(row), axis=1)
答案 0 :(得分:1)
我在这里使用cut()方法:
In [21]: df = pd.DataFrame(np.random.rand(10), columns=['A'])
In [22]: df
Out[22]:
A
0 0.513425
1 0.973631
2 0.549615
3 0.747600
4 0.099415
5 0.737613
6 0.885567
7 0.720187
8 0.446683
9 0.434688
In [23]: df['str_bos'] = pd.cut(df.A, bins=np.arange(0, 1.1, 0.1), labels=np.arange(10)+1)
In [24]: df
Out[24]:
A str_bos
0 0.513425 6
1 0.973631 10
2 0.549615 6
3 0.747600 8
4 0.099415 1
5 0.737613 8
6 0.885567 9
7 0.720187 8
8 0.446683 5
9 0.434688 5