3

是否有任何有效的方法来重塑数据框:

(A1、A2、A3、B1、B2、B3、C1、C2、C3、TT、YY 和 ZZ 是列)

A1 A2 A3 B1 B2 B3 C1 C2 C3 TT YY ZZ
11 22 33 44 55 66 77 88 99 23 24 25
11 22 33 44 55 66 77 88 99 23 24 25
11 22 33 44 55 66 77 88 99 23 24 25
11 22 33 44 55 66 77 88 99 23 24 25
11 22 33 44 55 66 77 88 99 23 24 25
11 22 33 44 55 66 77 88 99 23 24 25

至:

HH JJ KK TT YY ZZ
11 22 33 23 24 25
11 22 33 23 24 25
11 22 33 23 24 25
11 22 33 23 24 25
11 22 33 23 24 25
11 22 33 23 24 25
44 55 66 23 24 25
44 55 66 23 24 25
44 55 66 23 24 25
44 55 66 23 24 25
44 55 66 23 24 25
44 55 66 23 24 25
77 88 99 23 24 25
77 88 99 23 24 25
77 88 99 23 24 25
77 88 99 23 24 25
77 88 99 23 24 25
77 88 99 23 24 25

HH、JJ 和 KK 是新列,我将在其中制作 A、B、C 列的垂直堆叠并保持水平堆叠 TT、YY 和 ZZ

A1 A2 A3 TT YY ZZ 
B1 B2 B3 TT YY ZZ
C1 C2 C3 TT YY ZZ

谢谢你的帮助

4

2 回答 2

1

您可以使用列拆分和连接

df = pd.read_clipboard()
ColSets= [df.columns[i:i+3] for i in np.arange(0,len(df.columns)-3,3)]
LCols = df.columns[-3:]
NewDf = pd.concat([df[ColSet].join(df[LCols]).T.reset_index(drop=True).T for ColSet in ColSets])
NewDf.columns = ['HH', 'JJ', 'KK', 'TT', 'YY', 'ZZ']

出去:

HH  JJ  KK  TT  YY  ZZ
0   11  22  33  23  24  25
1   11  22  33  23  24  25
2   11  22  33  23  24  25
3   11  22  33  23  24  25
4   11  22  33  23  24  25
5   11  22  33  23  24  25
0   44  55  66  23  24  25
1   44  55  66  23  24  25
2   44  55  66  23  24  25
3   44  55  66  23  24  25
4   44  55  66  23  24  25
5   44  55  66  23  24  25
0   77  88  99  23  24  25
1   77  88  99  23  24  25
2   77  88  99  23  24  25
3   77  88  99  23  24  25
4   77  88  99  23  24  25
5   77  88  99  23  24  25
于 2020-04-09T09:59:31.287 回答
1

比以前的解决方案长一点:

#extract columns ending with numbers
abc = df.filter(regex='\d$')

#sort columns into separate lists
from itertools import groupby
from operator import itemgetter
cols = sorted(abc.columns,key=itemgetter(0))
filtered_columns = [list(g) for k,g in groupby(cols,key=itemgetter(0))]

#iterate through the dataframe
#and stack them
abc_stack = pd.concat([abc.filter(col)
                       .set_axis(['HH','JJ','KK'],axis='columns')
                       for col in filtered_columns],
                      ignore_index=True)

#filter for columns ending with alphabets
tyz = df.filter(regex= '[A-Z]$')

#get the dataframe to be the same length as abc_stack
tyz_stack = pd.concat([tyz] * len(filtered_columns),ignore_index=True)

#combine both dataframes
res = pd.concat([abc_stack,tyz_stack], axis=1)

res

    HH  JJ  KK  TT  YY  ZZ
0   11  22  33  23  24  25
1   11  22  33  23  24  25
2   11  22  33  23  24  25
3   11  22  33  23  24  25
4   11  22  33  23  24  25
5   11  22  33  23  24  25
6   44  55  66  23  24  25
7   44  55  66  23  24  25
8   44  55  66  23  24  25
9   44  55  66  23  24  25
10  44  55  66  23  24  25
11  44  55  66  23  24  25
12  77  88  99  23  24  25
13  77  88  99  23  24  25
14  77  88  99  23  24  25
15  77  88  99  23  24  25
16  77  88  99  23  24  25
17  77  88  99  23  24  25

更新:2021-01-08

重塑过程可以通过使用 pyjanitor 的pivot_longer函数来抽象;目前你必须从github安装最新的开发版本:

您共享的数据具有模式(有些列以 结尾1,有些以2结尾,有些以 结尾3),我们可以使用这些模式来重塑数据;

# install latest dev version
# pip install git+https://github.com/ericmjl/pyjanitor.git
 import janitor 

    (df.pivot_longer(names_to=("HH", "JJ", "KK"),
                     names_pattern=("1$", "2$", "3$"),
                     index=("TT", "YY", "ZZ")
                     )
       .sort_index(axis="columns"))

基本上,它的作用是查找以 结尾的列,将它们聚合为一列(“TT”),并对和1执行相同的操作。23

于 2020-04-09T10:19:01.877 回答