按值排序数据框并存储索引和列

时间:2017-03-28 12:26:02

标签: sorting pandas dataframe reshape

我有一个pandas DataFrame,它实际上是一个矩阵。它看起来如下所示

    a   b   c
d   1   0   5
e   0   6   2
f   2   0   3

我需要对值进行排序,并且需要索引和列的值。结果应该是

index Column Value

e       b      6

d       c      5

f       c      3

1 个答案:

答案 0 :(得分:1)

您需要stack重新塑造nlargest

import { NgModule, CUSTOM_ELEMENTS_SCHEMA }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule }    from '@angular/http';
import {FormsModule} from '@angular/forms';
import {NewsService} from './news.service'

import { AppComponent }  from './app.component';

import { NewsFeedComponent }  from './news-feed.component';

@NgModule({
    imports: [BrowserModule,HttpModule,FormsModule],
    declarations: [
    	AppComponent,
    	NewsFeedComponent
    	 ],
    providers: [NewsService],
    schemas: [CUSTOM_ELEMENTS_SCHEMA],
    bootstrap: [AppComponent]
})
export class AppModule { }

df1 = df.stack().nlargest(3).rename_axis(['idx','col']).reset_index(name='val') print (df1) idx col val 0 e b 6 1 d c 5 2 f c 3

MultiIndex