簡體   English   中英

Pandas:將多列添加到多索引列 dataframe

[英]Pandas: add multiple columns to a multiindex column dataframe

這個問題試圖概括為這個問題提供的解決方案:

Pandas:向多索引列添加一列 dataframe

我需要為每個列索引生成一列。

spencerlyon2提供的解決方案適用於我們要添加單列時:

df['bar', 'three'] = [0, 1, 2]

但是,我想為每個第一級列索引概括此操作。

來源 DF:

In [1]: df
Out[2]:
first        bar                 baz
second       one       two       one       two
A      -1.089798  2.053026  0.470218  1.440740
B       0.488875  0.428836  1.413451 -0.683677
C      -0.243064 -0.069446 -0.911166  0.478370

下面的目標DF,要求three列是其各自索引的one列和two列的相加。

In [1]: df
Out[2]:
first        bar                           baz                 
second       one       two     three       one       two      three
A      -1.089798  2.053026  0.963228‬  1.440740 -2.317647  -0.876907‬
B       0.488875  0.428836  0.917711 -0.683677  0.345873  -0.337804‬
C      -0.243064 -0.069446 -0.312510  0.478370  0.266761   0.745131‬

您可以使用join兩個具有相同索引的數據框來一次創建一堆列。


首先,使用groupbyaxis=1計算總和

ndf = df.groupby(df.columns.get_level_values(0), axis=1).sum()

        bar       baz
A  0.963228  1.910958
B  0.917711  0.729774
C -0.312510 -0.432796

(PS:如果你有兩個以上的列,你可以這樣做

df.loc[:, (slice(None), ['one', 'two'])].groupby(df.columns.get_level_values(0), axis=1).sum()

先只切片“一”和“二”列,然后groupby

然后,使其與您的列索引匹配,即使其成為 MultiIndexed 數據框,就像您的原始數據框一樣

ndf.columns = pd.MultiIndex.from_product([ndf.columns, ['three']])

        bar       baz
      three     three
A  0.963228  1.910958
B  0.917711  0.729774
C -0.312510 -0.432796

最后, df.join

finaldf = df.join(ndf).sort_index(axis=1)

如果您真的關心排序,請使用reindex

finaldf.reindex(['one', 'two', 'three'], axis=1, level=1)

first        bar                           baz                    
second       one       two     three       one       two     three
A      -1.089798  2.053026  0.963228  0.470218  1.440740  1.910958
B       0.488875  0.428836  0.917711  1.413451 -0.683677  0.729774
C      -0.243064 -0.069446 -0.312510 -0.911166  0.478370 -0.432796

我從您的示例輸入開始:

first        bar                 baz          
second       one       two       one       two
A      -1.089798  2.053026  0.470218  1.440740
B       0.488875  0.428836  1.413451 -0.683677
C      -0.243064 -0.069446 -0.911166  0.478370

要將新列添加到列 MultiIndex 的每個級別 0,您可以運行以下命令:

for c1 in df.columns.get_level_values('first').unique():
    # New column int index
    cInd = int(df.columns.get_loc(c1).stop)
    col = (c1, 'three')      # New column name
    newVal = df[(c1, 'one')] + df[(c1, 'two')]
    df.insert(loc=cInd, column=col, value=newVal)  # Insert the new column

在上面的示例中,新列中的值是連續的數字,但在您的情況下,可以根據需要設置它們。

我的代碼的結果(在列排序之后)是:

first        bar                           baz                    
second       one       two     three       one       two     three
A      -1.089798  2.053026  0.963228  0.470218  1.440740  1.910958
B       0.488875  0.428836  0.917711  1.413451 -0.683677  0.729774
C      -0.243064 -0.069446 -0.312510 -0.911166  0.478370 -0.432796

暫無
暫無

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

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