I have a data frame of 300 rows x 6 columns. I want to hstack every 50 rows x 6 columns to 1 row x 300 columns. Is there any way to do that?
CodePudding user response:
you can use this approach:
example data
df1 = pd.DataFrame([['C45','AB','ZU'],['C45','ZO','RE'],
['C67','RT','FG',],['C78','TZ','GH']], columns=['id1','id2','id3'])
it looks like this:
id1 id2 id3
0 C45 AB ZU
1 C45 ZO RE
2 C67 RT FG
3 C78 TZ GH
command
N = 2
pd.concat([df1.iloc[np.arange(i,len(df1),N)].reset_index(drop=True)
for i in range(N)], axis=1)
the result looks like this:
id1 id2 id3 id1 id2 id3
0 C45 AB ZU C45 ZO RE
1 C67 RT FG C78 TZ GH
you may also add rename
after reset_index
to avoid duplicate column names.