嗨,我想用给定的输入列表值对DataFrame列进行排序。 我的列表如下:
inputlist
[309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]
DataFrame是:
val kaywords
195 keyword3
221 keyword5
307 keyword8
309 keyword9
354 keyword0
426 keyword1
585 keyword2
698 keyword4
789 keyword33
在这里,我想根据给定的“输入列表”对DataFrame列“ val”进行排序。
我期望以下输出:
val kaywords
309 keyword9
585 keyword2
221 keyword5
789 keyword33
195 keyword3
354 keyword0
307 keyword8
698 keyword4
426 keyword1
答案 0 :(得分:4)
使用有序categorical
,但首先将list的值转换为整数:
inputlist = [309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]
df['val'] = pd.Categorical(df['val'], ordered=True, categories=[int(x) for x in inputlist])
df = df.sort_values('val')
print (df)
val kaywords
3 309 keyword9
6 585 keyword2
1 221 keyword5
8 789 keyword33
0 195 keyword3
4 354 keyword0
2 307 keyword8
7 698 keyword4
5 426 keyword1
如果val
中的所有值都存在于inputlist
中的另一种想法:
inputlist = [int(x) for x in inputlist]
df = df.set_index('val').reindex(inputlist).reset_index()
答案 1 :(得分:0)
input_list = [309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]
index = {int(j):i for i,j in enumerate(input_list)}
df = pd.DataFrame({"val":[195, 221, 307, 309, 354, 426, 585, 698, 789]})
df["sort"] = df.apply(lambda row: index[row.val], axis=1)
df = df.sort_values(by=['sort'])
df = df.drop("sort", axis=1)
答案 2 :(得分:0)
也许最简洁的方法是将val
定义为索引,然后按inputlist
的顺序调用它:
df.set_index('val').loc[inputlist, :]
答案 3 :(得分:0)
也许您可以创建一个新的数据框
inputlist=[309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]
df = pd.DataFrame({"val" :[195.0, 221, 307.0,309.0,354.0,426.0,585.0,698.0,789.0],
"keywords" :["keywords3","keywords5","keywords8","keywords9","keywords0","keywords1","keywords2","keywords4","keywords33"]
})
df2=pd.DataFrame()
for i in inputlist:
df2 = df2.append(df[df["val"]==i])
print(df2)
val keywords
3 309.0 keywords9
6 585.0 keywords2
1 221.0 keywords5
8 789.0 keywords33
0 195.0 keywords3
4 354.0 keywords0
2 307.0 keywords8
7 698.0 keywords4
5 426.0 keywords1