1

I have a dataset in the below format.

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

I would like to convert this into below:

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

How do I do this ? I tried numpy argsort but I am new to Python and finding this challenging. Appreciate any help in this.

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 list1=[['ind_cco_fin_ult1'], ['ind_cco_fin_ult1'], ['ind_deco_fin_ult1', 'ind_viv_fin_ult1'] ] list2=[['ind_cco_fin_ult1'], ['ind_cco_fin_ult1'], ['ind_ctma_fin_ult1', 'ind_deco_fin_ult1'] ]

```

python metrics.mapk(list2,list1,7)

0.83333333333333337

Thank you a lot for the help, I was able to try few steps. I am trying to test mapk but the apply method does not seem to give what I really need.

1 Answer 1

2

You can do something like this:

df.set_index("id").apply(lambda row: ' '.join(row[row == 1].index), axis = 1)

#id
#100      A
#101    B C
#102      A
#103    D E
#dtype: object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.