代码之家  ›  专栏  ›  技术社区  ›  Bijan

Pandas列出基于Groupby的所有唯一值

  •  1
  • Bijan  · 技术社区  · 11 月前

    我有一个数据框,里面有工地信息。

    District#    Site#           Address
            1        1    123 Bayview Ln
            1        2    456 Example St
            2       36      789 Hello Dr
            2       44      789 Hello Dr
    

    我正试图转换此数据帧,以便在按地区编号分组时添加一个具有最高站点编号和不同地址的列。以下是一个我希望输出的示例:

    District#    Site#           Address    MaxSite#            All District Addresses
            1        1    123 Bayview Ln           2     123 Bayview Ln,456 Example St
            1        2    456 Example St           2     123 Bayview Ln,456 Example St
            2       36      789 Hello Dr          44                      789 Hello Dr
            2       44      789 Hello Dr          44                      789 Hello Dr
    

    我可以通过以下操作获得最大站点号

    df['MaxSite#'] = df.groupby(by='District#')['Site#'].transform('max')
    

    但当我按地区编号分组时,我试图找到一种类似的方法来列出所有唯一的地址。

    我试过了 .transform('unique') 但这不是一个有效的函数名 .agg(['unique']) 返回不匹配的维度

    3 回复  |  直到 11 月前
        1
  •  4
  •   iBeMeltin    11 月前

    您可以使用 groupby agg 获取最大站点编号并列出所有地址

    那么 merge 返回原始数据帧:

    grouped_df = df.groupby('District#').agg(Max_Site_Num=('Site#', 'max'), 
          All_District_Addresses=('Address', lambda x: list(set(x))).reset_index()
    
    df = df.merge(grouped_df,on='District#')
    

    输出:

         District#  Site#         Address  Max_Site_Num            All_District_Addresses
    0          1      1  123 Bayview Ln             2  [123 Bayview Ln, 456 Example St]
    1          1      2  456 Example St             2  [123 Bayview Ln, 456 Example St]
    2          2     36    789 Hello Dr            44                    [789 Hello Dr]
    3          2     44    789 Hello Dr            44                    [789 Hello Dr]
    
        2
  •  2
  •   Capybara    11 月前

    获取unqiue地址有两个步骤。

    1. 我们获得每个地址对应的唯一地址 District# 并将它们组合成一个字符串
    2. 然后我们创建 All District Addresses 按映射系列 地区# unique_addresses DataFrame。
    data = {
     "District#": [1, 1, 2, 2],
     "Site#": [1, 2, 36, 44],
     "Address": ["123 Bayview Ln", "456 Example St", "789 Hello Dr", "789 Hello Dr"]
    }
    df = pd.DataFrame(data)
    # Series you already calculated
    df['MaxSite#'] = df.groupby(by='District#')['Site#'].transform('max') 
    
    # Adding per-district unique addresses 
    unique_addresses = df.groupby(by="District#")["Address"].unique().apply(lambda x: ",".join(x))  # to make values a single string
    df["All District Addresses"] = df["District#"].map(unique_addresses)
    

    输出:

       District#  Site#         Address  MaxSite#         All District Addresses
    0          1      1  123 Bayview Ln         2  123 Bayview Ln,456 Example St
    1          1      2  456 Example St         2  123 Bayview Ln,456 Example St
    2          2     36    789 Hello Dr        44                   789 Hello Dr
    3          2     44    789 Hello Dr        44                   789 Hello Dr
    

    如果你喜欢,可以加一句:

    df["All District Addresses"] = df["District#"].map(df.groupby(by="District#")["Address"].unique().apply(lambda x: ",".join(x)))
    
        3
  •  2
  •   e-motta    11 月前

    使用 transform :

    df["Max_Site_Num"] = df.groupby("District#")["Site#"].transform("max")
    df["All_District_Addresses"] = df.groupby("District#")["Address"].transform(
        lambda x: ",".join(set(x))
    )
    
       District#  Site#         Address  Max_Site_Num         All_District_Addresses
    0          1      1  123 Bayview Ln             2  456 Example St,123 Bayview Ln
    1          1      2  456 Example St             2  456 Example St,123 Bayview Ln
    2          2     36    789 Hello Dr            44                   789 Hello Dr
    3          2     44    789 Hello Dr            44                   789 Hello Dr
    
    推荐文章