Reputation: 7255
Here's my data
customer_id value
var_name Value_1 Value_2
0 1 70 80
I want to make it
var_name customer_id Value_1 Value_2
0 1 70 80
what I did is df.columns = ['customer_id', 'Value_1', 'Value_2']
but this is to prone to mistake since I had hundreds of columns
How suppose I do this?
Upvotes: 1
Views: 48
Reputation: 862591
You always get MultiIndex
, because no parameter values
with one value:
df = data.pivot_table(index='customer_id',
columns='var_name',
fill_value=0,
aggfunc='max')
#columns names of first level are not removed
df.columns = df.columns.map('_'.join)
#first level removed, but possible duplicated columns names, so not recommended
#df.columns = df.columns.droplevel(0)
df = df.reset_index()
If scalar in value
(processes one column only):
df = data.pivot_table(index='customer_id',
columns='var_name',
fill_value=0,
values='age',
aggfunc='max')
Upvotes: 1