import pandas as pd
import numpy as np
df=pd.DataFrame({'customer':[165,265,288,268,296],
'R_shirt':[np.nan,1.0,np.nan,1.0,np.nan],
'B_shirt':[np.nan,np.nan,2.0,np.nan,np.nan],
'X_shirt':[5.0,np.nan,2.0,np.nan,np.nan],
'Y_shirt':[3.0,np.nan,2.0,3.0,np.nan]
})
print(df)
customer R_shirt B_shirt X_shirt Y_shirt
0 165 NaN NaN 5.0 3.0
1 265 1.0 NaN NaN NaN
2 288 NaN 2.0 2.0 2.0
3 268 1.0 NaN NaN 3.0
4 296 NaN NaN NaN NaN
df['customer']=df['customer'].astype(str)
df=df.pivot_table(columns='customer')
customer = '165'
print(df)
customer 165 265 268 288
B_shirt NaN NaN NaN 2.0
R_shirt NaN 1.0 1.0 NaN
X_shirt 5.0 NaN NaN 2.0
Y_shirt 3.0 NaN 3.0 2.0
best_for_customer=df[customer][df[customer]!=np.nan].to_frame().sort_values(by=customer,ascending=False).dropna()
print(best_for_customer)
165
X_shirt 5.0
Y_shirt 3.0
переменная customer
- это имя клиента, которое вы хотите проверить