【问题标题】:How to aggregate 3 columns in DataFrame to have count and distribution of values in separated columns in Python Pandas?如何聚合 DataFrame 中的 3 列以在 Python Pandas 中的分隔列中计算和分布值?
【发布时间】:2022-12-17 16:58:57
【问题描述】:

我有如下所示的 Pandas DataFrame:

数据类型:

  • ID - 整数

  • 时间 - 整数

  • TG-整数

    ID TIME TG
    111 20210101 0
    111 20210201 0
    111 20210301 1
    222 20210101 0
    222 20210201 1
    333 20210201 1

我需要在 DataFrame 上方进行汇总,以便了解:

  1. TIME 中每个值有多少个 ID
  2. TIME 中每个值有多少个来自 TG 的“1”
  3. TIME 中每个值有多少个来自 TG 的“0”

    所以我需要像下面这样的东西:

    TIME     | num_ID | num_1 | num_0
    ---------|--------|-------|--------
    20210101 | 2      | 0     | 2
    20210201 | 3      | 2     | 1
    20210301 | 1      | 1     | 0
    

    我怎样才能在 Python Pandas 中做到这一点?

【问题讨论】:

    标签: python pandas dataframe group-by aggregation


    【解决方案1】:
    import pandas as pd
    
    # Create the DataFrame
    df = pd.DataFrame({
        'ID': [111, 111, 111, 222, 222, 333],
        'TIME': [20210101, 20210201, 20210301, 20210101, 20210201, 20210201],
        'TG': [0, 0, 1, 0, 1, 1]
    })
    
    # Group the DataFrame by the 'TIME' column
    grouped_df = df.groupby('TIME')
    
    # Aggregate the grouped DataFrame and create a new DataFrame
    # that counts the number of IDs, number of 1s and number of 0s
    # for each value in the 'TIME' column
    result_df = grouped_df.agg({
        'ID': 'nunique',  # Count the number of unique IDs
        'TG':'sum' 
    }).rename(columns={'ID': 'num_ID', 'TG': 'num_1'})
    
    # Calculate the number of 0s in the 'TG' column
    # by subtracting the number of 1s from the total number of entries
    result_df['num_0'] = grouped_df['TG'].count() - result_df['num_1']
    
    # Reorder the columns in the result DataFrame
    result_df = result_df[['num_ID', 'num_1', 'num_0']]
    
    # Print the result DataFrame
    print(result_df)
    

    【讨论】:

      【解决方案2】:

      使用GroupBy.size计算TIME值,使用crosstab计算01值:

      df1 = (df.groupby('TIME').size().to_frame('num_ID')
               .join(pd.crosstab(df['TIME'], df['TG']).add_prefix('num_'))
               .reset_index())
      print (df1)
             TIME  num_ID  num_0  num_1
      0  20210101       2      2      0
      1  20210201       3      1      2
      2  20210301       1      0      1
      

      如果只需要计算 GroupBy.agg 中的 01 值,另一个想法是:

      df1 = (df.assign(num_0 = df['TG'].eq(0),
                      num_1 = df['TG'].eq(1))
              .groupby('TIME').agg(num_ID = ('TG','size'),
                                   num_1=('num_1','sum'),
                                   num_0=('num_0','sum'),
                                   )
              .reset_index()
              )
      print (df1)
             TIME  num_ID  num_1  num_0
      0  20210101       2      0      2
      1  20210201       3      2      1
      2  20210301       1      1      0
      

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 2020-09-26
        • 2021-04-14
        • 1970-01-01
        • 2014-12-03
        • 1970-01-01
        • 2022-07-29
        相关资源
        最近更新 更多