Reputation: 9338
A dataframe as below and I want to convert it to one row, with new columns created from its original rows and columns,
data = {'Contract' : ["Team A", "Team B", "Team C"],
'Revenue': [11,7,10],
'Cost' : [5,2,9],
'Tax' : [4,2,2]}
like:
I tried:
import pandas as pd
df = pd.DataFrame(data)
print (df.values.flatten())
The result is not ideal:
['Team A' 5L 11L 4L 'Team B' 2L 7L 2L 'Team C' 9L 10L 2L]
How can I achieve it?
Upvotes: 1
Views: 27
Reputation: 323226
Check
s = df.set_index('Contract').stack().to_frame(0).T
s.columns=s.columns.map('_'.join)
s
Team A_Revenue Team A_Cost ... Team C_Cost Team C_Tax
0 11 5 ... 9 2
[1 rows x 9 columns]
Upvotes: 1