[英]Turn M x (n*N) into (n*M) x N dataframe
I have a dataframe df1
with M
rows and n*N
columns. 我有一个具有M
行和n*N
列的数据df1
。 The columns are named 列名为
1.1,...,1.N,2.1,...,2.N,...n.1,...nN
The rows are indexed 1,...M
. 这些行的索引为1,...M
What is an elegant way to turn this into a new dataframe df2
with n*M rows indexed 1.1,...,1.M,...,n.1,...nM
and N
columns named 1,...,N
, such that 有什么df2
方法将其转换为新的数据帧df2
其中n * M行的索引为1.1,...,1.M,...,n.1,...nM
和N
列名为1,...,N
,这样
df2.iloc[i*M+j,:]==df1.iloc[j,i*n:(i+1)*N]
Use Series.str.split
with DataFrame.stack
and last flatten MultiIndex
by f-string
s: 使用Series.str.split
与DataFrame.stack
和最后一个压平MultiIndex
由f-string
S:
df.columns = df.columns.str.split('.', expand=True)
df = df.stack(0)
df.index = [f'{a}.{b}' for a, b in df.index]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.