Jason Boyd
Jason Boyd

Reputation: 7019

How do reindex multilevel columns

Version info:

print(sys.version)
3.5.1 |Anaconda 4.1.0 (64-bit)| (default, Jun 15 2016, 15:29:36) [MSC v.1900 64 bit (AMD64)]

I have columns in a data frame that look like this (latitude and longitude are multilevel columns):

+------------+---------------+--------------+--------------+
| CustomerId | StreetAddress |   Latitude   |   Longitude  | 
+------------+---------------+-------+------+-------+------+
|                            | count | mean | count | mean |
+----------------------------+-------+------+-------+------+

I would like to get this:

+------------+---------------+-----------+----------+-----------+----------+
| CustomerId | StreetAddress | Lat_count | Lat_mean | Lon_count | Lon_mean | 
+------------+---------------+-----------+----------+-----------+----------+

I tried this:

newColumns = ['CustomerId','StreetAddress','Lat_count','Lat_mean','Lon_count','Lon_mean']
data2 = data1.reindex(columns=newColumns)

But that absolutely did not work! I ended up with some kind of crazy multilevel columns with each letter of each string in newColumns being a new level.

Update

Here are my columns

data1.columns.to_series()

CustomerId                  (CustomerId, )
StreetAddress            (StreetAddress, )
Latitude       count     (Latitude, count)
               mean       (Latitude, mean)
Longitude      count    (Longitude, count)
               mean      (Longitude, mean)

Upvotes: 4

Views: 212

Answers (1)

benten
benten

Reputation: 1991

This will do the trick:

data2 = pd.DataFrame(data1.values, columns=newColumns)

And also this:

data1.columns = newColumns

Upvotes: 4

Related Questions