在datafram上使用聚合

2024-05-28 23:01:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据框,我需要对我在值中列出的一个特定列使用聚合函数。我用的是熊猫的透视表。你知道吗

 Sample ID  Type    Score   Freq
AE01    AAA Non 0.65    1
AE01    BBB IND 0.57    14
AE03    SAS IND 0.56    14
AE03    SAP IND 0.689   15
AE03    TCS IND 0.56    16
AE05    BBB IND 0.85    17
AE05    CTC IND 0.45    18
AE05    CTC Non 0.15    19
AE05    CTC Non 0.14    20
AE05    CTC Non 0.4678  21

下面是我用的剧本

table_pat_rel = pd.pivot_table(df,index=["ID",'Type'],values=['Sample'],
               aggfunc={'Sample':np.size})

给出以下输出

ID  Type    Sample
AAA Non 1
BBB IND 2
SAS IND 1
SAP IND 1
TCS IND 1
CTC IND 5

但我的目标是

ID  Recurrence  Sample
AAA 1   AE01
BBB 2   AE01 
        AE05
SAS 1   AE03
SAP 1   AE03
TCS 1   AE03
CTC 4   AE05

我试着用groupby如下

 df.drop_duplicates(['Sample', 'ID']).groupby(['ID','Sample']).size().sort_values(ascending=True).head()

Tags: sampleidtypetablesapbbbnonsas
1条回答
网友
1楼 · 发布于 2024-05-28 23:01:11

数据:

df = pd.DataFrame(
{'Score': [0.65, 0.57, 0.56, 0.689, 0.56, 0.85, 0.45, 0.15, 0.14, 0.4678], 
'ID': ['AAA', 'BBB', 'SAS', 'SAP', 'TCS', 'BBB', 'CTC', 'CTC', 'CTC', 'CTC'], 
'Sample': ['AE01', 'AE01', 'AE03', 'AE03', 'AE03', 'AE05', 'AE05', 'AE05', 'AE05', 'AE05'], 
'Freq': [1, 14, 14, 15, 16, 17, 18, 19, 20, 21], 
'Type': ['Non', 'IND', 'IND', 'IND', 'IND', 'IND', 'IND', 'IND', 'IND', 'IND']},
columns=['Sample','ID','Type','Score','Freq'])
print (df)
  Sample   ID Type   Score  Freq
0   AE01  AAA  Non  0.6500     1
1   AE01  BBB  IND  0.5700    14
2   AE03  SAS  IND  0.5600    14
3   AE03  SAP  IND  0.6890    15
4   AE03  TCS  IND  0.5600    16
5   AE05  BBB  IND  0.8500    17
6   AE05  CTC  IND  0.4500    18
7   AE05  CTC  IND  0.1500    19
8   AE05  CTC  IND  0.1400    20
9   AE05  CTC  IND  0.4678    21

orig = pd.pivot_table(df,index=["ID",'Type'],values=['Sample'],
               aggfunc={'Sample':np.size})

print (orig)
          Sample
ID  Type        
AAA Non        1
BBB IND        2
CTC IND        4
SAP IND        1
SAS IND        1
TCS IND        1

我认为您需要交换SampleType,而不是values=['Sample']使用values=['Freq'],但似乎可以使用其他一些不用于index的列,因为使用aggfunc=len(与aggfunc='size'相同)

table_pat_rel1 = pd.pivot_table(df,index=["ID",'Sample'],values=['Freq'],aggfunc=len) \
                  .reset_index(level=1) \
                  .rename(columns={'Freq':'Recurrence'}) \
                  .set_index('Recurrence', append=True)
print (table_pat_rel1)
               Sample
ID  Recurrence       
AAA 1            AE01
BBB 1            AE01
    1            AE05
CTC 4            AE05
SAP 1            AE03
SAS 1            AE03
TCS 1            AE03

或者将^{}与聚合^{}一起使用:

table_pat_rel2 = df.groupby(['ID','Sample']) \
                   .size() \
                   .reset_index(level=1) \
                   .rename(columns={0:'Recurrence'}) \
                   .set_index('Recurrence', append=True)

print (table_pat_rel2)
               Sample
ID  Recurrence       
AAA 1            AE01
BBB 1            AE01
    1            AE05
CTC 4            AE05
SAP 1            AE03
SAS 1            AE03
TCS 1            AE03

相关问题 更多 >

    热门问题