Zifendale
Zifendale

Reputation: 140

Renaming Columns from pivot_table

I run a pivot_table on a dataframe and the resulting column names are:

('firstvar', 8001)    ('firstvar', 8002)    ('secondvar', 8001)    ('secondvar', 8002)

But before I can use the data I need to rename the columns to something along these lines:

firstvar_8001    firstvar_8002    secondvar_8001    secondvar_8002

I tried to see if this could be done when doing the pivot table but I don't think it can be. So instead I'm looking for a solution to renaming the columns in the dataframe before I save it as a csv

I need to run this on multiple files where the 8000 codes might change so I was thinking of using regex but I can't find any support for renaming columns in a dataframe using regex.

Edit: So I realize now that the names are actually python tuples and I'm working on converting them to strings and joining them now.

Edit: So I found a clumsy solution. My issue was that it wouldn't allow me to rename the tuples so I ended up just exporting and re-import to flatten the levels and then renaming them in that fashion. I really don't like the solution though so if anyone has a suggestion for a better way to do it; I would really appreciate it!

Upvotes: 0

Views: 4911

Answers (1)

ragesz
ragesz

Reputation: 9527

Late answer but can be helpful for others...

my_colnames = []
for i, a in enumerate(my_pivoted_table.columns.get_level_values(1)):
    my_colnames.append(str(a) + '_' + str(my_pivoted_table.columns.get_level_values(0)[i]))
my_pivoted_table.columns = my_colnames

Upvotes: 3

Related Questions