我想遍历多个dataframe列,寻找每个列中的前n个值。如果列中的值在前n个值中,则保留该值,否则将bucket保存在“other”中。另外,我还想从中创建新的列。在
但是,我不确定在这种情况下如何使用.apply
,因为似乎我需要同时引用列和行。在
np.random.seed(0)
example_df = pd.DataFrame(np.random.randint(low=0, high=10, size=(15, 5)),columns=['a', 'b', 'c', 'd', 'e'])
cols_to_group = ['a','b','c']
top = 2
对于下面的示例,下面是我不确定如何执行的伪代码:
伪代码:
^{2}$预期产量:
粗略的例子,其中top=2。在
a b c d e a_bucketed b_bucketed
0 4 6 4 3 1 4 6
1 8 8 1 5 7 8 8
2 8 6 0 0 2 8 6
3 4 1 0 7 4 4 Other
4 7 8 7 7 7 Other 8
有一种方法。但目前还没有对领带进行治疗。在
相关问题 更多 >
编程相关推荐