如何将特定范围的列展平为 python 中的一列?
how to flatten certain range of columns into one in python?
我有一个数据 table,维度为 214 行和 972 列。
我想每 108 列拾取一次,然后展平成一列。
那么输出 table 的维度为 23112 (=214*108) 行和 9 列。
我试过pd.concat
和pd.flatten
等等,,,,但是我只能将table的整列拼合成一列。
为了简单起见,下面是玩具table。拿起每两列并将它们压平成一列。所以我想重塑; [ 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,太难搞懂了,
谢谢您的帮助!
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
您也可以使用 , 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
使用列表理解,可以在一行中完成:
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)
在阅读其他人对您的问题的回答之前,我会这样做:
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)
我有一个数据 table,维度为 214 行和 972 列。
我想每 108 列拾取一次,然后展平成一列。
那么输出 table 的维度为 23112 (=214*108) 行和 9 列。
我试过pd.concat
和pd.flatten
等等,,,,但是我只能将table的整列拼合成一列。
为了简单起见,下面是玩具table。拿起每两列并将它们压平成一列。所以我想重塑; [ 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,太难搞懂了, 谢谢您的帮助!
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
您也可以使用 , 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
使用列表理解,可以在一行中完成:
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)
在阅读其他人对您的问题的回答之前,我会这样做:
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)