2016-12-15 8 views
1

Ich habe einen Datensatz im folgenden Format.Python3, Spaltennamen aus Array - numpy oder Pandas

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 

Ich möchte dies unten in umwandeln:

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

Wie mache ich das? Ich habe versucht, numpy argsort, aber ich bin neu in Python und finde das herausfordernd. Schätzen Sie jede Hilfe in diesem.

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 

Sie für die Hilfe viel Dank, ich war in der Lage wenige Schritte zu versuchen. Ich versuche mapk zu testen, aber die Methode apply scheint nicht zu geben, was ich wirklich brauche.

Antwort

2

Sie können etwas tun:

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 
Verwandte Themen