我有以下代碼。
我正在嘗試按升序對“happydflist”資料框的第一列的值進行排序。
但是,這給我的輸出包括一些不符合升序主題的值,例如“2”、“3”和“8”。
happydflist = happydflist[happydflist.columns[0]]
happydflistnew = happydflist.sort_values(ascending=True)
print(happydflistnew)
12 13
10 19
13 2
11 24
15 3
6 33
24 35
8 36
5 37
25 49
17 49
20 50
26 51
22 52
16 52
18 52
19 52
28 53
27 54
23 54
21 59
9 74
7 75
14 8
Name: 0_happy, dtype: object
我將非常感謝您的幫助!
'happydflist' 看起來像這樣:
5 37
6 33
7 75
8 36
9 74
10 19
11 24
12 13
13 2
14 8
15 3
16 52
17 49
18 52
19 52
20 50
21 59
22 52
23 54
24 35
25 49
26 51
27 54
28 53
Name: 0_happy, dtype: object
uj5u.com熱心網友回復:
也許您的資料框的某些資料型別是str
. 使int
happydflist.astype('int').sort_values()
如果你需要dtypestr
使用astype
1more
happydflist.astype('int').sort_values().astype('str')
uj5u.com熱心網友回復:
我設法通過使用 df.strip() 函式來洗掉資料框中文本周圍的“空白”,并結合 .dropna() 函式來解決這個問題。
happydflistnew = happydflist[happydflist.columns[0]].str.strip()
happydflistnew = happydflistnew.dropna()
happydflistsorted = happydflistnew.astype('int').sort_values(ascending=True)
maxvalue = len(happydflistsorted)
minhappiness = happydflistsorted.iloc[0]
maxhappiness = happydflistsorted.iloc[maxvalue-1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/532440.html
上一篇:計算熊貓資料框列的累積計數