簡體   English   中英

在 Pandas 數據框中取消旋轉多個具有相同名稱的列

[英]Unpivot multiple columns with same name in pandas dataframe

我有以下數據框:

pp  b          pp   b
5   0.001464    6   0.001853
5   0.001459    6   0.001843

有沒有辦法將具有相同名稱的列反透視為多行?

這是所需的輸出:

pp  b         
5   0.001464    
5   0.001459    
6   0.001853
6   0.001843

嘗試groupby與軸 = 1

df.groupby(df.columns.values, axis=1).agg(lambda x: x.values.tolist()).sum().apply(pd.Series).T.sort_values('pp')
Out[320]: 
          b   pp
0  0.001464  5.0
2  0.001459  5.0
1  0.001853  6.0
3  0.001843  6.0

使用wide_to_long的有趣方式

s=pd.Series(df.columns)
df.columns=df.columns+s.groupby(s).cumcount().astype(str)

pd.wide_to_long(df.reset_index(),stubnames=['pp','b'],i='index',j='drop',suffix='\d+')
Out[342]: 
            pp         b
index drop              
0     0      5  0.001464
1     0      5  0.001459
0     1      6  0.001853
1     1      6  0.001843

這可以使用numpy

res = pd.DataFrame({'pp': df['pp'].values.T.ravel(),
                    'b': df['b'].values.T.ravel()})

print(res)

          b  pp
0  0.001464   5
1  0.001459   5
2  0.001853   6
3  0.001843   6

或者不明確引用特定列:

res = pd.DataFrame({i: df[i].values.T.ravel() for i in set(df.columns)})

讓我們使用melt、cumcount和unstack:

dm = df.melt()
dm.set_index(['variable',dm.groupby('variable').cumcount()])\
  .sort_index()['value'].unstack(0)

輸出:

variable         b   pp
0         0.001464  5.0
1         0.001459  5.0
2         0.001853  6.0
3         0.001843  6.0

我有點驚訝到目前為止沒有人提到 pd.concat 的使用......看看下面:

df1 = pd.DataFrame({'Col1':[1,2,3,4], 'Col2':[5,6,7,8]})
df1
      Col1  Col2
   0     1     5
   1     2     6
   2     3     7
   3     4     8 

現在如果你做:

   df2 = pd.concat([df1,df1])

你得到:

   Col1  Col2
0     1     5
1     2     6
2     3     7
3     4     8
0     1     5
1     2     6
2     3     7
3     4     8

這就是你想要的,不是嗎?

如果您知道前面的重復次數,則使用 numpy 很容易:

import numpy as np
import pandas as pd

repetitions=5
rows=2
original_columns=list('ab')

df=pd.DataFrame(np.random.randint(0,10,[rows,len(original_columns)*repetitions]), columns=original_columns*repetitions)
display(df)

    a   b   a   b   a   b   a   b   a   b
0   6   4   7   5   2   5   3   1   4   3
1   1   5   4   9   6   2   9   5   3   6

# now the interesting part:
df=pd.concat(np.hsplit(df, repetitions))
display(df)


    a   b
0   6   4
1   1   5
0   7   5
1   4   9
0   2   5
1   6   2
0   3   1
1   9   5
0   4   3
1   3   6

暫無
暫無

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

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