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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.