Python3,数组numpy或pandas中的列名

2024-05-15 14:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下格式的数据集。你知道吗

id  A   B   C   D   E
100 1   0   0   0   0
101 0   1   1   0   0
102 1   0   0   0   0
103 0   0   0   1   1

我想将此转换为以下内容:

100, A
101, B C
102, A
103, D E

我该怎么做?我试过numpy argsort,但我对Python还不熟悉,发现这很有挑战性。 谢谢你在这方面的帮助。你知道吗

python df3 = df1.set_index("cust_id").apply(lambda col: ','.join(col[lambda x: x == 1].index), axis = 1)

python df3

cust_id
1375586                      ind_cco_fin_ult1
1050611                      ind_cco_fin_ult1
1050612    ind_deco_fin_ult1,ind_viv_fin_ult1
dtype: object

python df2

cust_id
1375586                       ind_cco_fin_ult1
1050611                       ind_cco_fin_ult1
1050612    ind_ctma_fin_ult1,ind_deco_fin_ult1
dtype: object

python metrics.mapk(df2,df3,7)

0.82879818594104293

``` python 列表1=[['ind\u cco\u fin\u ult1'],['ind\u cco\u fin\u ult1'],['ind\u deco\u fin\u ult1','ind\u viv\u fin\u ult1']] 列表2=[['ind\u cco\u fin\u ult1'],['ind\u cco\u fin\u ult1'],['ind\u ctma\u fin\u ult1','ind\u deco\u fin\u ult1']]

````

python metrics.mapk(list2,list1,7)

0.83333333333333337

非常感谢你的帮助,我试了几步。 我试图测试mapk,但apply方法似乎没有给出我真正需要的东西。你知道吗


Tags: lambdaidindexcolapplydtypedecoind