I want to concatenate multiple columns in a DataFrame into a single column and transpose them into a single row, but when I call transpose it remains a single column.
import pandas as pd
df = pd.DataFrame(
{
0: [1, 2, 3],
1: [11, 12, 13]
}
)
print( df)
df = pd.concat( [df[0], df[1]])
df = df.T.copy()
print( '--------')
print( df)
Even with "df = df.T.copy()" (even without it), after transpose, as follows;
0 1
0 1 11
1 2 12
2 3 13
--------
0 1
1 2
2 3
0 11
1 12
2 13
dtype: int64
Below is the shape I want.
0 1 2 3 4 5
0 1 2 3 11 12 13
Please, somebody give me some advice.
CodePudding user response:
Dump down into numpy and build a new dataframe:
import pandas as pd
import numpy as np
out = df.to_numpy().ravel(order='F')
pd.DataFrame([out])
0 1 2 3 4 5
0 1 2 3 11 12 13
CodePudding user response:
example:
df = pd.DataFrame(
{
0: [1, 2, 3],
1: [11, 12, 13]
}
)
use following code:
n = df.shape[0] * df.shape[1]
pd.DataFrame(df.values.reshape((1, n), order='F'))