![](/img/trans.png)
[英]Sort the rows of a dataframe and get the column values in pandas dataframe
[英]Sort rows and get column IDs in a pandas dataframe
使用給定的pandas數據幀,我想為連續的最高,第二高,第三高等值創建新列。 然后為每個列的相應列名創建另一列。 下面的代碼對行的最大值執行此操作,但不執行以下操作。
import pandas as pd
df = pd.DataFrame({'A': (23, 24, 55, 77, 33, 66),
'B': (12, 33, 0.2, 44, 23.5, 66),
'C': (1, 33, 66, 44, 5, 62),
'D': (9, 343, 4, 64, 24, 63),
'E': (123, 33, 2.2, 42, 2, 99)})
# Determine the max value and column name and add as columns to df
df['Max1'] = df.max(axis=1)
df['Col_Max1'] = df.idxmax(axis=1)
# Determine the 2nd and 3rd max PR and threshold levels and add as columns
# ???????????
print(df)
這會產生:
A B C D E Max1 Col_Max1
0 23 12.0 1 9 123.0 123.0 E
1 24 33.0 33 343 33.0 343.0 D
2 55 0.2 66 4 2.2 66.0 C
3 77 44.0 44 64 42.0 77.0 A
4 33 23.5 5 24 2.0 33.0 A
5 66 66.0 62 63 99.0 99.0 E
Process finished with exit code 0
唯一需要注意的是,如果對性能有影響,可能會有非常多的列。 多謝你們。
使用關注性能的底層陣列數據的一種方法是 -
a = df.values
c = df.columns
idx = a.argsort(1)[:,::-1]
vals = a[np.arange(idx.shape[0])[:,None], idx]
IDs = c[idx]
names_vals = ['Max'+str(i+1) for i in range(a.shape[1])]
names_IDs = ['Col_Max'+str(i+1) for i in range(a.shape[1])]
df_vals = pd.DataFrame(vals, columns=names_vals)
df_IDs = pd.DataFrame(IDs, columns=names_IDs)
df_out = pd.concat([df, df_vals, df_IDs], axis=1)
樣本輸入,輸出 -
In [40]: df
Out[40]:
A B C D E
0 23 12.0 1 9 123.0
1 24 33.0 33 343 33.0
2 55 0.2 66 4 2.2
3 77 44.0 44 64 42.0
4 33 23.5 5 24 2.0
5 66 66.0 62 63 99.0
In [41]: df_out
Out[41]:
A B C D E Max1 Max2 Max3 Max4 Max5 Col_Max1 Col_Max2 \
0 23 12.0 1 9 123.0 123.0 23.0 12.0 9.0 1.0 E A
1 24 33.0 33 343 33.0 343.0 33.0 33.0 33.0 24.0 D E
2 55 0.2 66 4 2.2 66.0 55.0 4.0 2.2 0.2 C A
3 77 44.0 44 64 42.0 77.0 64.0 44.0 44.0 42.0 A D
4 33 23.5 5 24 2.0 33.0 24.0 23.5 5.0 2.0 A D
5 66 66.0 62 63 99.0 99.0 66.0 66.0 63.0 62.0 E B
Col_Max3 Col_Max4 Col_Max5
0 B D C
1 C B A
2 D E B
3 C B E
4 B C E
5 A D C
如果您需要按順序排列值和ID,我們需要修改其中的最后幾個步驟 -
df0 = pd.DataFrame(np.dstack((vals, IDs)).reshape(a.shape[0],-1))
df0.columns = np.vstack((names_vals, names_IDs)).T.ravel()
df_out = pd.concat([df, df0], axis=1)
樣品輸出 -
In [62]: df_out
Out[62]:
A B C D E Max1 Col_Max1 Max2 Col_Max2 Max3 Col_Max3 Max4 \
0 23 12.0 1 9 123.0 123 E 23 A 12 B 9
1 24 33.0 33 343 33.0 343 D 33 E 33 C 33
2 55 0.2 66 4 2.2 66 C 55 A 4 D 2.2
3 77 44.0 44 64 42.0 77 A 64 D 44 C 44
4 33 23.5 5 24 2.0 33 A 24 D 23.5 B 5
5 66 66.0 62 63 99.0 99 E 66 B 66 A 63
Col_Max4 Max5 Col_Max5
0 D 1 C
1 B 24 A
2 E 0.2 B
3 B 42 E
4 C 2 E
5 D 62 C
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.