【问题标题】:how to flatten certain range of columns into one in python?如何在python中将特定范围的列扁平化为一个?
【发布时间】:2020-06-29 06:40:36
【问题描述】:

我有一个维度为 214 行和 972 列的数据表。

我想每 108 列拾取一次,然后压平成一列。

那么输出表的维度将是 23112 (=214*108) 行和 9 列。

我已经尝试过pd.concat 和pd.flatten 之类的东西,,,,但我只能将表格的整列扁平化为一列。

简单来说,下面是玩具桌。拿起每两列并将它们展平成一列。所以我想重塑; [5*6]到[10*3]

a  b  c  d  e  f 
g  h  i  j  k  l
m  n  o  p  q  r 
s  t  u  v  w  x
y  z  1  2  3  4 

这将被转换为

a  c  e
g  i  k
m  o  q
s  u  w
y  1  3
b  d  f
h  j  l
n  p  r
t  v  x
z  2  4

我对python真的很陌生,,,,太难弄明白了,, 谢谢您的帮助!

【问题讨论】:

    标签: python pandas datatable flatten


    【解决方案1】:

    你也可以使用,np.reshape:

    n = 2 #replace n with your factor
    pd.DataFrame(np.reshape(df.to_numpy().T,(df.shape[1]//n,df.shape[0]*n)).T)
    

    甚至不使用 类似 Fortran 的索引顺序进行整形

    pd.DataFrame(np.reshape(df.to_numpy(),(df.shape[0]*n,df.shape[1]//n),order='F'))
    

       0  1  2
    0  a  c  e
    1  g  i  k
    2  m  o  q
    3  s  u  w
    4  y  1  3
    5  b  d  f
    6  h  j  l
    7  n  p  r
    8  t  v  x
    9  z  2  4
    

    【讨论】:

      【解决方案2】:

      IIUC:

      print (pd.concat([pd.DataFrame(df.iloc[:,::2].values),
                        pd.DataFrame(df.iloc[:,1::2].values)])
             .reset_index(drop=True))
      

      或者用数字 n 的列表推导进行概括:

      n=2
      print (pd.concat([pd.DataFrame(df.iloc[:,i::n].values) for i in range(n)])
             .reset_index(drop=True))
      
         0  1  2
      0  a  c  e
      1  g  i  k
      2  m  o  q
      3  s  u  w
      4  y  1  3
      5  b  d  f
      6  h  j  l
      7  n  p  r
      8  t  v  x
      9  z  2  4
      

      【讨论】:

        【解决方案3】:

        使用列表推导,可以在一行中完成:

        cols_to_flatten = 2 #change this as per your requirement
        pd.concat([pd.concat([df[col] for col in df.columns[i:i+cols_to_flatten]], ignore_index=True) for i in range(0, df.shape[1], cols_to_flatten)], axis=1)
        

        【讨论】:

          【解决方案4】:

          在阅读其他人对您的问题的回答之前,我会这样做:

          import pandas as pd
          
          
          def concat_every(frame, every):
              frame = frame.copy()
              new_df = pd.DataFrame(
                  columns=range(len(frame.columns)//every)
              )
              for c in new_df.columns:
                  col = frame.columns
                  new_df[c] = pd.concat(
                      [frame.pop(col[i]) for i in range(every)],
                      ignore_index=True
                  )
              return new_df
          
          
          df = pd.DataFrame({
              0:['a','g','m','s','y'],
              1:['b','h','n','t','z'],
              2:['c','i','o','u',1],
              3:['d','j','p','v',2],
              4:['e','k','q','w',3],
              5:['f','l','r','x',4],
          })
          
          df1 = concat_every(df, 2)
          
          print(df)
          print(df1)
          

          Python Tutor Link To Code

          【讨论】:

            猜你喜欢
            • 2012-05-24
            • 2010-11-18
            • 1970-01-01
            • 2019-05-29
            • 2019-04-23
            • 1970-01-01
            • 2018-05-14
            • 2010-12-13
            • 2013-01-17
            相关资源
            最近更新 更多