for example, I have a table like this:
a b
e 20 30
d 60 50
f 120 10
my desired result is:
a_e a_d a_f b_e b_d b_f
1 20 60 120 30 50 10
I tried redo the crosstab and pivot table, but it doesn't work.
CodePudding user response:
You can use pandas.DataFrame.values
and flatten
to convert it to your desired format
Creating data
df = pd.DataFrame({'a' : [20,60,120], 'b':[30,50,10]}, index = ['e', 'd', 'f'])
Generating output
pd.DataFrame([df.T.values.flatten()], columns = [f"{col}_{ind}" for col in df.columns for ind in df.index])
Output
This gives us the expected output
a_e a_d a_f b_e b_d b_f
0 20 60 120 30 50 10