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