簡體   English   中英

Groupby Diff - Pandas

[英]Groupby Diff - Pandas

我想找到多索引中列之間的區別,我有三個維度,家庭、日期和客戶,目標是在 Multi 中擁有與客戶、日期和家庭不同的行的新列-指數。

    import pandas as pd
    import numpy as np

    data = {
        'Family':{
            0: 'Hugo',
            1: 'Hugo', 
            2: 'Hugo', 
            3: 'Hugo'},
        'Date': {
            0: '2021-04-15',
            1: '2021-04-16',
            2: '2021-04-15',
            3: '2021-04-16'},
        'Client': {
            0: 1,
            1: 1,
            2: 2,
            3: 2},
        'Code_Client': {
            0: 605478.0,
            1: 605478.0,
            2: 605478.0,
            3: 605478.0},
        'Price': {
            0: 2.23354416539888,
            1: 2.0872536032616744,
            2: 1.8426286431701764,
            3: 0.3225935619590472}
        }

    df = pd.DataFrame(data)
    pd.pivot_table(pd.DataFrame(data), values='Price', index=['Code_Client'],columns= 
    ['Family','Date', 'Client'])

在此處輸入圖像描述

你有什么主意嗎?

謝謝,

我假設您正在尋找按FamilyDate and Client分組的 Price 的差異。 您對問題的表述有些不清楚,並且您沒有發布預期的 output。 我稍微更改了您的 dataframe 以添加一個系列以使解決方案更加明顯。

data = {
        'Family':{
            0: 'Hugo',
            1: 'Hugo', 
            2: 'Victor', 
            3: 'Victor'},
        'Date': {
            0: '2021-04-15',
            1: '2021-04-16',
            2: '2021-04-15',
            3: '2021-04-16'},
        'Client': {
            0: 1,
            1: 1,
            2: 2,
            3: 2},
        'Code_Client': {
            0: 605478.0,
            1: 605478.0,
            2: 605478.0,
            3: 605478.0},
        'Price': {
            0: 2.23354416539888,
            1: 2.0872536032616744,
            2: 1.8426286431701764,
            3: 0.3225935619590472}
        }

    df = pd.DataFrame(data)
    pd.pivot_table(pd.DataFrame(data), values='Price', index=['Code_Client'],columns= 
    ['Family','Date', 'Client'])

如您所見,我添加了 Victor 家族。 所以,你的 dataframe 看起來像這樣:

Family        Date  Client  Code_Client     Price
0    Hugo  2021-04-15       1     605478.0  2.233544
1    Hugo  2021-04-16       1     605478.0  2.087254
2  Victor  2021-04-15       2     605478.0  1.842629
3  Victor  2021-04-16       2     605478.0  0.322594

要按組添加差異列,我建議您執行以下操作:

df =  df.set_index(['Family', 'Date','Client']).sort_index()[['Price']]
df['diff'] = np.nan
idx = pd.IndexSlice

for ix in df.index.levels[0]:
    df.loc[ idx[ix,:], 'diff'] = df.loc[idx[ix,:], 'Price' ].diff()

第一步索引您的變量(您要分組的變量)並創建一個空的(或用nan填充的)差異列。 第二步通過行之間的差異,按組填充它。

這將返回:

                       Price      diff
Family Date       Client                    
Hugo   2021-04-15 1       2.233544       NaN
       2021-04-16 1       2.087254 -0.146291
Victor 2021-04-15 2       1.842629       NaN
       2021-04-16 2       0.322594 -1.520035

如果您對nan不滿意,請執行以下操作:

df =  df.set_index(['Family', 'Date','Client']).sort_index()[['Price']]
df['diff'] = np.nan
idx = pd.IndexSlice

for ix in df.index.levels[0]:
    df.loc[ idx[ix,:], 'diff'] = df.loc[idx[ix,:], 'Price' ].diff().fillna(0)

我將.fillna(0)添加到diff()語句中。 它返回:

                     Price      diff
Family Date       Client                    
Hugo   2021-04-15 1       2.233544  0.000000
       2021-04-16 1       2.087254 -0.146291
Victor 2021-04-15 2       1.842629  0.000000
       2021-04-16 2       0.322594 -1.520035
​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM