Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 对于python中给定的值条件,在dataframe中以列表形式返回列名_Python 2.7_List_Pandas_Dataframe - Fatal编程技术网

Python 2.7 对于python中给定的值条件,在dataframe中以列表形式返回列名

Python 2.7 对于python中给定的值条件,在dataframe中以列表形式返回列名,python-2.7,list,pandas,dataframe,Python 2.7,List,Pandas,Dataframe,给定1xN dataframe表,需要从行中选择5个最大值,并将相应的列名返回到列表中。 这是dataframe示例: 5 2 13 15 37 8 89 PageRank 0.444384 0.44453 0.444695 0.444882 0.444759 0.44488 0.444648 试过这个, r = list(pr.loc['PageRank'

给定1xN dataframe表,需要从行中选择5个最大值,并将相应的列名返回到列表中。 这是dataframe示例:

            5        2         13         15         37        8         89    
PageRank  0.444384  0.44453  0.444695  0.444882  0.444759  0.44488  0.444648
试过这个,

r = list(pr.loc['PageRank'].nlargest(5))
但是创建的列表只有行中的值,而没有列名。 如何获取5个最大单元格值的列名? 例如,在给定的数据帧中,它应该返回

[15,37,13,89,5]

使用
索引

r1 = list(pr.loc['PageRank'].nlargest(5).index)
print (r1)
[15, 8, 37, 13, 89]
或:


通过使用Numpy的
np.argpartition
,您可以获得一些额外的性能。为了得到正确的方向,我将在负值上使用它

我想使用
np.argpartition
而不是排序,因为它是
O(n)
而不是
O(nlogn)
排序


定时
您会注意到,
pir1
的性能优于。但也要注意,
nlargest
逐渐接近
pir1
的性能,因为它们都是
O(n)

定时比率
此表显示了每个方法的时间与该特定长度数组所用最小时间的比率

res.div(res.min(1), 0)

              jez1       jez2       jez3  pir1
10       20.740497   8.666576   6.738210   1.0
30       39.325125  11.962184  10.987012   1.0
100      30.121521  10.184435  10.173252   1.0
300      58.544734  11.963354  12.563072   1.0
1000     63.643729   9.361290   8.547374   1.0
3000     22.041026  15.977949  18.803516   1.0
10000     9.254778  11.620570  11.681464   1.0
30000     2.838243   7.522210   7.120721   1.0
100000    1.814005   7.486602   6.995017   1.0
300000    1.920776  13.213261  12.423890   1.0
1000000   1.332265   7.872120   7.225150   1.0

如果数组足够大,以至于前5个列中出现平局,这是意料之中的。你和我一样多,所以不知道有什么问题,但在更大的数据帧中是个问题。我不明白,那么解决方案可以吗?我在30、50列的小随机数据帧中测试它,有时返回与
nlargest
相同的输出,有时不返回。你能再解释一下吗?谢谢。我试着为此创建,但被否决了,所以看起来很愚蠢(
cols = pr.columns.values
rnks = -pr.values[0]
cols[np.argpartition(rnks, 5)[:5]].tolist()

['37', '15', '13', '8', '89']
jez1 = lambda d: list(d.loc['PageRank'].nlargest(5).index)
jez2 = lambda d: d.columns[d.loc['PageRank'].values.argsort()[::-1]][:5].tolist()
jez3 = lambda d: d.columns[d.loc['PageRank'].values.argsort()[-1:-6:-1]].tolist()
pir1 = lambda d: d.columns.values[np.argpartition(-d.values[0], 5)[:5]].tolist()

res = pd.DataFrame(
    index=[10, 30, 100, 300, 1000, 3000, 10000, 30000, 100000, 300000, 1000000],
    columns='jez1 jez2 jez3 pir1'.split(),
    dtype=float
)

for i in res.index:
    d = pd.DataFrame(dict(PageRank=np.random.rand(i))).T
    for j in res.columns:
        stmt = '{}(d)'.format(j)
        setp = 'from __main__ import d, {}'.format(j)
        res.at[i, j] = timeit(stmt, setp, number=200)

res.plot(loglog=True)
res.div(res.min(1), 0)

              jez1       jez2       jez3  pir1
10       20.740497   8.666576   6.738210   1.0
30       39.325125  11.962184  10.987012   1.0
100      30.121521  10.184435  10.173252   1.0
300      58.544734  11.963354  12.563072   1.0
1000     63.643729   9.361290   8.547374   1.0
3000     22.041026  15.977949  18.803516   1.0
10000     9.254778  11.620570  11.681464   1.0
30000     2.838243   7.522210   7.120721   1.0
100000    1.814005   7.486602   6.995017   1.0
300000    1.920776  13.213261  12.423890   1.0
1000000   1.332265   7.872120   7.225150   1.0