【问题标题】:Iterate over multiindex dataframe groups with unequal second index level迭代具有不等第二索引级别的多索引数据帧组
【发布时间】:2021-07-14 01:34:04
【问题描述】:

我有一个 MultiIndex (Name, Date) DataFrame df,我需要通过 Date 对其进行迭代处理,以便分配一个基于当前和前一个日期组的值。

当我在不同日期有不同的Name 集时,我不知道该怎么做。我在下面的循环中用注释HELP!!! 标记了有问题的行:

    df = pd.DataFrame(data=[('A', '20210101', 5.0),  # 1 Jan
                            ('B', '20210101', 3.0),
                            ('C', '20210101', 2.0),
                            ('A', '20210102', 0.0),  # 2 Jan
                            ('C', '20210102', 0.0),
                            ('A', '20210103', 0.0),  # 3 Jan
                            ('C', '20210103', 0.0),
                            ('D', '20210103', 0.0)],
                      columns=('Name', 'Date', 'Dollars')).set_index(['Name', 'Date'])

    # Logic: Each day total wealth sums to $10.
    # Each day: Each person starts with what he had the previous day; 
    #           excess wealth gets allocated evenly to everyone
    
    dft = df.groupby(df.index.get_level_values('Date'))
    dates = list(dft.groups.keys())
    # Initialize first group:
    previous = dft.get_group(dates[0])

    # Loop over groups in order:
    for date in dates[1:]:
        current = dft.get_group(date)
        current.Dollars = previous.Dollars  # << HELP!!!
        excess = 10.0 - current.Dollars.sum()
        current.Dollars = current.Dollars + excess / current.Dollars.count()
        # Assign the calculated values back to the DataFrame:
        df.loc[current.index] = current
        # Prepare for next iteration:
        previous = current

在HELP!!! 点,我需要以某种方式完成以下操作:

  1. 将current.Dollars 分配给previous.Dollars,忽略current 和previous 上的Date 索引。即使我执行.reset_index(level=1),鉴于Name 索引每天都可能发生变化,我看不到如何完成此任务– 请注意,在示例df 中,第二天输了 em> 行名B,第三天有一个新行名D。

  2. 在执行循环中的逻辑时忽略Date 级别后,我需要以某种方式恢复它以将结果分配回相应Date 上的主df。


在这个例子中,循环结束后 `df` 的值应该是:
               Dollars
Name Date             
A    20210101      5.0
B    20210101      3.0
C    20210101      2.0
A    20210102      6.5
C    20210102      3.5
A    20210103      6.5
C    20210103      3.5
D    20210103      0.0

【问题讨论】:

    标签: pandas dataframe multi-index


    【解决方案1】:

    如简化版here所示, 您必须操作索引才能进行分配,然后将值返回到原始 DataFrame:

    # Initialize first group, but take Date out of its index:
    previous = dft.get_group(dates[0]).reset_index(level=1)
    
    for date in dates[1:]:
        # Take the Date out of the index:
        current = dft.get_group(date).reset_index(level=1)
    
        # Get the common keys
        commonKeys = current.index.intersection(previous.index)
    
        # Do the "left assignment"
        current.loc[commonKeys, 'Dollars'] = previous.loc[commonKeys].Dollars
    
        # Do your calculations:
        excess = 10.0 - current.Dollars.sum()
        current.Dollars = current.Dollars + excess / current.Dollars.count()
    
        # Restore the Date column to the index:
        current.set_index('Date', append=True, inplace=True)
    
        # Assign the calculated values back to the DataFrame:
        df.loc[current.index] = current
    
        # Prepare for next iteration by removing the Date index again:
        previous = current.reset_index(level=1)
    

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2014-11-13
      • 2022-07-21
      • 2013-03-16
      • 1970-01-01
      • 2017-05-20
      • 2019-01-22
      • 2019-01-24
      • 2021-12-13
      相关资源
      最近更新 更多