5 回答

TA貢獻1830條經(jīng)驗 獲得超9個贊
使用:
i = np.argsort(df.to_numpy() * -1, axis=1)
r = pd.DataFrame(df.columns[i], index=df.index, columns=range(1, i.shape[1] + 1))?
df = df.join(r.add_prefix('Rank'))
細節(jié):
使用np.argsort
along獲取將按降序?qū)α髋蛇M行排序的axis=1
索引。i
print(i) array([[1,?3,?2,?0], ???????[2,?3,?1,?0], ???????[2,?3,?1,?0], ???????[1,?2,?3,?0], ???????[2,?3,?1,?0]])
從沿著索引(即)獲取的r
數(shù)據(jù)框的列創(chuàng)建一個新的數(shù)據(jù)框,然后使用連接數(shù)據(jù)框:df
i
df.columns[i]
DataFrame.join
r
df
print(df)
? ? ? ? ? ? ? Jazz? Dance? Music? Theatre? Rank1? ? Rank2? ? Rank3 Rank4
Customer? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
100000000001? ? ?0? ? ? 3? ? ? 1? ? ? ? 2? Dance? Theatre? ? Music? Jazz
100000000002? ? ?0? ? ? 1? ? ? 6? ? ? ? 2? Music? Theatre? ? Dance? Jazz
100000000003? ? ?0? ? ? 3? ? ?13? ? ? ? 4? Music? Theatre? ? Dance? Jazz
100000000004? ? ?0? ? ? 5? ? ? 4? ? ? ? 1? Dance? ? Music? Theatre? Jazz
100000000005? ? ?1? ? ?10? ? ?16? ? ? ?14? Music? Theatre? ? Dance? Jazz

TA貢獻1725條經(jīng)驗 獲得超8個贊
嘗試這個:
dfp = (df.rank(ascending=False, axis=1).stack()
.astype(int).rename('rank').reset_index(level=1))
df.assign(**dfp.set_index('rank', append=True)['Genre'].unstack().add_prefix('Rank'))
輸出:
Genre Jazz Dance Music Theatre Rank1 Rank2 Rank3 Rank4
Customer
100000000001 0 3 1 2 Dance Theatre Music Jazz
100000000002 0 1 6 2 Music Theatre Dance Jazz
100000000003 0 3 13 4 Music Theatre Dance Jazz
100000000004 0 5 4 1 Dance Music Theatre Jazz
100000000005 1 10 16 14 Music Theatre Dance Jazz
使用rank并重塑數(shù)據(jù)框,然后使用assign.

TA貢獻1815條經(jīng)驗 獲得超10個贊
讓我們試試stack,cumcount和sort_values:
s = df.stack().sort_values(ascending=False).groupby(level=0).cumcount() + 1
s1 = (s.reset_index(1)
.set_index(0, append=True)
.unstack(1)
.add_prefix("Rank")
)
s1.columns = s1.columns.get_level_values(1)
然后加入您的客戶類型索引。
df.join(s1)
Jazz Dance Music Theatre Rank1 Rank2 Rank3 Rank4
Customer_Genre
100000000001 0 3 1 2 Dance Theatre Music Jazz
100000000002 0 1 6 2 Music Theatre Dance Jazz
100000000003 0 3 13 4 Music Theatre Dance Jazz
100000000004 0 5 4 1 Dance Music Theatre Jazz
100000000005 1 10 16 14 Music Theatre Dance Jazz

TA貢獻1836條經(jīng)驗 獲得超4個贊
上述解決方案有效,但我們現(xiàn)在收到以下棄用警告。
r = pd.DataFrame(df.columns[i], index=df.index, columns=range(1, i.shape[1] + 1))
FutureWarning:對多維索引(例如obj[:, None]
)的支持已棄用,并將在未來版本中刪除。在索引之前轉(zhuǎn)換為 numpy 數(shù)組。
修訂:r = pd.DataFrame(np.array(df.columns)[i], index=df.index, columns=range(1, i.shape[1] + 1))

TA貢獻1831條經(jīng)驗 獲得超4個贊
這是一個改進以前答案的函數(shù),考慮到以下幾點:
它通過在索引它們之前將 df.columns 轉(zhuǎn)換為 numpy 數(shù)組來解決 Wally 提到的棄用警告。
它還允許包含 NaN 值并避免將這些列用于排名列(也將它們的值保留為 NaN)。檢查示例。
它還添加了相應的排名值以輕松映射它們。
如果您想按升序或降序?qū)λ鼈冞M行排名,則有一個附加參數(shù)。
添加一個附加列,指定哪些列具有 NaN 值并且未包含在排名列中。這些值被添加到列表中。
# Example DataFrame
import numpy as np
import pandas as pd
dic = {'A': [0, np.nan, 2, np.nan],
'B': [3, 0, 1, 5],
'C': [1, 2, 0, np.nan]}
df = pd.DataFrame(dic)
print(df)
A B C
0 0.0 3 1.0
1 NaN 0 2.0
2 2.0 1 0.0
3 NaN 5 NaN
# Function
def fun_rank_columns(df, ascending=False):
factor = 1 if ascending else -1
# Rank columns showing ranking of column names
np_sort = np.argsort(df.to_numpy() * factor, axis=1)
df_rank = pd.DataFrame(np.array(df.columns)[np_sort], index=df.index, columns=range(1, np_sort.shape[1] + 1))
# Corresponding values for each rank column
np_sort_value = np.sort(df.to_numpy() * factor, axis=1)
df_rank_value = pd.DataFrame(np_sort_value, index=df.index, columns=range(1, np_sort_value.shape[1] + 1)) * factor
# Columns with nan values to be replaced
num_col_rank = df_rank.shape[1]
df_rank['nan_value'] = df.apply(lambda row: [i for i in df.columns if np.isnan(row[i])], axis=1)
for col in range(1, num_col_rank + 1):
condition = df_rank.apply(lambda x: x[col] in x['nan_value'], axis=1)
df_rank.loc[condition, col] = np.nan
df_rank_value.loc[condition, col] = np.nan
# Join Results
df_rank = df_rank.add_prefix('rank_')
df_rank_value = df_rank_value.add_prefix('rank_value_')
df_res = df_rank.join(df_rank_value)
return df_res
# Apply the function
df_res = fun_rank_columns(df, ascending=True)
print(df_res)
rank_1 rank_2 rank_3 rank_nan_value rank_value_1 rank_value_2 rank_value_3
0 A C B [] 0.0 1.0 3.0
1 B C NaN [A] 0.0 2.0 NaN
2 C B A [] 0.0 1.0 2.0
3 B NaN NaN [A, C] 5.0 NaN NaN
添加回答
舉報