2
我如何(對於比所提供示例大得多的矩陣有效地)返回最大的n
的列名稱和索引(或行名稱)或最小值返回熊貓數據系列中第n個最大值的索引和列名稱
import pandas as pd
import numpy as np
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
matrix = df.corr()
matrix
A B C D
A 1.000000 -0.814913 0.495993 -0.880296
B -0.814913 1.000000 -0.211421 0.551441
C 0.495993 -0.211421 1.000000 -0.414037
D -0.880296 0.551441 -0.414037 1.000000
然後,我會做一些如
def get_n_smallest(matrix, n):
# can return as two variables, list, tuple, whatever...
return row_name, col_name
get_n_smallest(matrix,0)
# would return D, A for the value -.880296
@JohnGalt但隨後這僅僅是最低的,而不是第n個最低 – thefoxrocks
真,怎麼樣'matrix.unstack()sort_values()指數[。 n-1]'爲第n小? – Zero