Ste*_*era 2 sorting indexing series pandas
我遇到问题时,想按列对数据框进行排序
df = pd.DataFrame(np.random.random(5))
df.sort(columns = [0],inplace = True)
df
0
4 0.275867
1 0.304362
2 0.625837
0 0.741176
3 0.767829
Run Code Online (Sandbox Code Playgroud)
当我对列进行排序时,得到的结果不是我所期望的,我期望索引仍应为0、1、2、3、4,所以我如何获得该结果并使列0保持升序。
我认为您需要reset_index带参数drop=True:
np.random.seed(1)
df = pd.DataFrame(np.random.random(5))
df.sort_values(by=[0],inplace = True)
print (df)
0
2 0.000114
4 0.146756
3 0.302333
0 0.417022
1 0.720324
df.reset_index(drop=True, inplace=True)
print (df)
0
0 0.000114
1 0.146756
2 0.302333
3 0.417022
4 0.720324
Run Code Online (Sandbox Code Playgroud)