How multi-level reindex columns

Version Information:

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 multi-level columns):

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

I would like to get the following:

+------------+---------------+-----------+----------+-----------+----------+
| 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 it absolutely didn't work! I ended up with some crazy layered columns, each letter of each row in newColumnshas become 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)
+4
source share
1 answer

This will do the trick:

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

And also this:

data1.columns = newColumns
+3
source

Source: https://habr.com/ru/post/1649971/


All Articles