显示每组顶部和底部 N 个值的 Pandas 新数据框

cec*_*ilj 2 python dataframe pandas

我有一个包含三列的 DataFrame:单位、影响因素和价值。有几种不同类型的影响者,这些值代表单位的数量。我想创建一个新表,显示每个影响者的最频繁和最不频繁的 n 个单位及其各自的值。

我的 df 看起来像这样:

Unit    Influencer    Value
A       foo           321
B       foo           200
C       foo           20
D       foo           12
E       foo           3
A       bar           999
B       bar           209
C       bar           89
D       bar           34
E       bar           15
F       bar           2
Run Code Online (Sandbox Code Playgroud)

我的输出应该看起来像(假设我们想要顶部和底部 2 个单位):

Unit    Influencer    Value
    A       foo           321
    B       foo           200
    D       foo           12
    E       foo           3
    A       bar           999
    B       bar           209
    E       bar           15
    F       bar           2
Run Code Online (Sandbox Code Playgroud)

我尝试了类似于此处找到的解决方案的方法,但出现错误“索引包含重复条目,无法重塑”,我认为这是因为“影响者”是我的 df 的索引。如果我的 df 是 multiindex,则创建了新的 df,但它是不正确的。

def get_top3(counts, col1, col2):

    top3 = (counts.groupby(col1))[col2].apply(lambda x: x.nlargest(3)).reset_index(level=1, drop=True).to_frame('VAL')

    top3 = counts.set_index(np.arange(len(counts)) % 3, append=True)['value'].unstack().add_prefix('VAL')

    return top3
Run Code Online (Sandbox Code Playgroud)

但是,这会创建一个如下所示的 Dataframe:

VAL1  VAL2  VAL3
321   NaN   NaN
NaN   200   NaN
NaN   NaN   20
12    NaN   NaN
NaN   3     NaN
...
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激!我也愿意接受关于如何格式化我的输出 df 的反馈。谢谢!

Dan*_*ejo 5

你可以试试:

nlargest = df.groupby('Influencer')['Value'].nlargest(2).reset_index()['level_1'].values
nsmallest = df.groupby('Influencer')['Value'].nsmallest(2).reset_index()['level_1'].values

result = pd.concat([df.iloc[nlargest], df.iloc[nsmallest]]).sort_index()
print(result)
Run Code Online (Sandbox Code Playgroud)

输出

   Unit Influencer  Value
0     A        foo    321
1     B        foo    200
3     D        foo     12
4     E        foo      3
5     A        bar    999
6     B        bar    209
9     E        bar     15
10    F        bar      2
Run Code Online (Sandbox Code Playgroud)


WeN*_*Ben 5

使用

#df=df.sort_values('Value')
g=df.groupby('Influencer')
pd.concat([g.head(2),g.tail(2)]).sort_index()
Out[693]: 
   Unit Influencer  Value
0     A        foo    321
1     B        foo    200
3     D        foo     12
4     E        foo      3
5     A        bar    999
6     B        bar    209
9     E        bar     15
10    F        bar      2
Run Code Online (Sandbox Code Playgroud)