如何在pandas中并排连接n行

q5lcpyga  于 11个月前  发布在  其他
关注(0)|答案(6)|浏览(75)

我想将同一数据集的五行合并合并为一个数据集,我有700行,我想每隔五行进行合并合并:

A  B  C  D  E  F   G
1     10,11,12,13,14,15,16    
2     17,18,19,20,21,22,23    
3     24,25,26,27,28,29,30      
4     31,32,33,34,35,36,37    
5     38,39,40,41,42,43,44
.
.
.
.
.
700

字符串
合并前五行后..我的第一行应该是这样的:

A  B  C  D  E  F  G  A  B  C  D  E  F  G  A  B  C  D  E  F  G  A  B  C  D  E  F  G  A  B  C  D  E  F  G
                                                                         
    1  10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44

ruoxqz4g

ruoxqz4g1#

如果你能保证你拥有的总行数是5的倍数,那么使用numpy将是解决这个问题的最有效的方法:

import numpy as np
import pandas as pd

data = np.arange(70).reshape(-1, 7)
df = pd.DataFrame(data, columns=[*'ABCDEFG'])

print(df)
    A   B   C   D   E   F   G
0   0   1   2   3   4   5   6
1   7   8   9  10  11  12  13
2  14  15  16  17  18  19  20
3  21  22  23  24  25  26  27
4  28  29  30  31  32  33  34
5  35  36  37  38  39  40  41
6  42  43  44  45  46  47  48
7  49  50  51  52  53  54  55
8  56  57  58  59  60  61  62
9  63  64  65  66  67  68  69

out = pd.DataFrame(
    df.to_numpy().reshape(-1, df.shape[1] * 5),
    columns=[*df.columns] * 5
)

print(out)
    A   B   C   D   E   F   G   A   B   C   D   E   F  ...   B   C   D   E   F   G   A   B   C   D   E   F   G
0   0   1   2   3   4   5   6   7   8   9  10  11  12  ...  22  23  24  25  26  27  28  29  30  31  32  33  34
1  35  36  37  38  39  40  41  42  43  44  45  46  47  ...  57  58  59  60  61  62  63  64  65  66  67  68  69

[2 rows x 35 columns]

字符串

o3imoua4

o3imoua42#

您可以:

cols = [col for v in [df.columns.tolist()]*len(df) for col in v]
dfs = [df[i:min(i+5,len(df))].reset_index(drop=True) for i in range(0,len(df),5)]
df2 = pd.concat([pd.DataFrame(df.stack()).T for df in dfs])
df2.columns = cols
df2.reset_index(drop=True, inplace=True)

字符串

svmlkihl

svmlkihl3#

看看这是否有助于回答你的问题unstack把列变成行,一旦我们有了列中的数据,我们只需要把它转置。reset_index把结果序列变成一个数组。原始的列名变成一个索引,所以当我们转置时,我们有了你在列中声明的列。

df.unstack().reset_index().set_index('level_0')[[0]].T

个字符
投票和/或接受,如果答案有帮助

6ljaweal

6ljaweal4#

最简单的方法是将你的数组转换成一个numpy数组,重新塑造它,然后将它转换回一个新的数组。
编辑:

data= # your dataframe
new_dataframe=pd.DataFrame(data.to_numpy().reshape(len(data)//5,-1),columns=np.tile(data.columns,5))

字符串

piok6c0g

piok6c0g5#

pandas中数据的堆叠和拆堆

表格中的数据通常以多种方式呈现。长格式(“整洁数据”)指的是堆叠在几列中的数据。其中一列将具有关于值的分类指示符。相比之下,宽格式(“堆叠数据”)是每个类别都有自己的列。
在您的示例中,您呈现了宽格式的数据,并试图将其转换为长格式。pandas.melt、pandas.groupby、pandas.pivot、pandas.stack、pandas.unstack和pandas.reset_index是帮助在这些格式之间进行转换的函数。

从您的原始框架开始:

df = pd.DataFrame({
   'A' : [10, 17, 24, 31, 38],
   'B' : [11, 18, 25, 32, 39],
   'C' : [12, 19, 26, 33, 40],
   'D' : [13, 20, 27, 34, 41],
   'E' : [14, 21, 28, 35, 42],
   'F' : [15, 22, 29, 36, 43],
   'G' : [16, 23, 30, 37, 44]})

    A   B   C   D   E   F   G
0   10  11  12  13  14  15  16
1   17  18  19  20  21  22  23
2   24  25  26  27  28  29  30
3   31  32  33  34  35  36  37
4   38  39  40  41  42  43  44

字符串

**使用pandas.melt将其转换为长格式,然后排序以获得您请求的数据:**忽略索引选项有助于我们稍后将其恢复为宽格式。

melted_df = df.melt(ignore_index=False).sort_values(by='value')

variable    value
0   A   10
0   B   11
0   C   12
0   D   13
0   E   14
0   F   15
0   G   16
1   A   17
1   B   18
...

**使用groupby、unstack和reset_index将其转换回宽格式。**这通常是一个困难得多的过程,它依赖于按值堆叠列、其他列、索引和堆叠变量分组,然后取消堆叠并重置索引。

(melted_df
    .reset_index() # puts the index values into a column called 'index'
    .groupby(['index','variable']) #groups by the index and the variable
    .value  #selects the value column in each of the groupby objects
    .mean() #since there is only one item per group, it only aggregates one item
    .unstack() #this sets the first item of the multi-index to columns
    .reset_index() #fix the index
    .set_index('index') #set index
)
    A   B   C   D   E   F   G                           
0   10  11  12  13  14  15  16
1   17  18  19  20  21  22  23
2   24  25  26  27  28  29  30
3   31  32  33  34  35  36  37
4   38  39  40  41  42  43  44


这些东西可能相当困难,需要反复试验。我建议你把问题做得更小,然后把它们弄乱。这样你就可以弄清楚函数是如何工作的。

kqhtkvqz

kqhtkvqz6#

尝试使用arange()floordiv来获得每5个的组,然后用组创建一个新的df。即使你的df不能被5整除,这也应该有效。

l = 5
(df.groupby(np.arange(len(df.index))//l)
 .apply(lambda x: pd.DataFrame([x.to_numpy().ravel()]))
 .set_axis(df.columns.tolist() * l,axis=1)
 .reset_index(drop=True))

字符串

(df.groupby(np.arange(len(df.index))//5)
.apply(lambda x: x.reset_index(drop=True).stack())
.unstack(level=[1,2])
.droplevel(0,axis=1))


输出量:

A  B  C  D  E  F  G  A  B  C  ...  E  F  G  A  B  C  D  E  F  G
0  9  0  3  2  6  2  9  1  7  5  ...  2  5  9  5  4  9  7  3  8  9
1  9  5  0  8  1  5  8  7  7  7  ...  6  3  5  5  2  3  9  7  5  6

相关问题