熊猫SeriesGroupBy.nlargest重复多级索引

时间:2019-01-20 10:26:00

标签: python pandas group-by pandas-groupby

我有熊猫系列
它具有名称为useritem的MultiIndex:

>>> s
user  item
11    1       0.60
      3       0.46
      4       0.68
      2       0.75
      0       0.05
13    1       0.71
      3       0.82
      4       0.65
      2       0.45
      0       0.16
12    1       0.39
      3       0.06
      4       0.09
      2       0.96
      0       0.97
0     1       0.31
      3       0.27
      4       0.22
      2       0.52
      0       0.06
5     1       0.03
      3       0.47
      4       0.34

我想为每个用户获取2个最大值。我想要相同格式的结果。

但是当我这样做时,却得到了user的重复索引

>>> s.groupby('user').nlargest(2)
user  user  item
11    11    2       0.75
            4       0.68
13    13    3       0.82
            1       0.71
12    12    0       0.97
            2       0.96
0     0     2       0.52
            1       0.31
5     5     0       0.90
            3       0.47
7     7     1       0.84
            4       0.84
6     6     1       0.91

1 个答案:

答案 0 :(得分:0)

添加procedure TFMain.Button1Click(Sender: TObject); var lHTTP: TIdHTTP; res: string; begin lHTTP := TIdHTTP.Create; try res := lHTTP.Get(edApi.Text); Memo1.Lines.Add(res); finally lHTTP.Free; end; end;

procedure TFMain.Button3Click(Sender: TObject);
var
   HTTP : TIDHTTP;
   Cookie : TidCookieManager;
   res: string;
begin
   try
      HTTP := TIDHTTP.Create(nil);
      Cookie := TidCookieManager.Create(nil);

      HTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
      HTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
      HTTP.Request.AcceptLanguage := 'en-us;q=0.7,en;q=0.3';
      HTTP.Request.AcceptCharSet := 'windows-1251,utf-8;q=0.7,*;q=0.7';
      HTTP.Request.Pragma := 'no-cache';
      HTTP.Request.CacheControl := 'no-cache';
      HTTP.Request.RawHeaders.Add('X-Requested-With: XMLHttpRequest');
      HTTP.AllowCookies := True;
      HTTP.HandleRedirects := True;
      HTTP.ProtocolVersion := pv1_1;
      HTTP.Request.Connection := 'Keep-Alive';
      HTTP.CookieManager  := Cookie;

      res := http.Get(edApi.Text);
      Memo1.Lines.Clear;
      Memo1.Lines.Add(res);
   finally
      FreeAndNil(Cookie);
      FreeAndNil(HTTP);
   end;
end;