python - 如何重新索引多级列

标签 python windows pandas

版本信息:

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)]

我在数据框中有如下所示的列(纬度和经度是多级列):

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

我想得到这个:

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

我试过这个:

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

但这绝对行不通!我最终得到了一些疯狂的多级列,newColumns 中每个字符串的每个字母都是一个新级别。

更新

这是我的专栏

data1.columns.to_series()

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

最佳答案

这样就可以了:

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

还有这个:

data1.columns = newColumns

关于python - 如何重新索引多级列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732188/

相关文章:

python - Paypal,加密的添加到购物车按钮动态生成

python - 我正在尝试找到第 n 个二进制回文

python - keras ImageDataGenerator 内插二进制掩码

c++ - 如何使用 C/C++ 检索可移动存储盘符

.net - 为什么 .NET 3.5 垃圾收集器卡住我的应用程序?

python - CouchDB 只写/只读(不可编辑)用户

python - 无法使用Python连接到MSSQL Server数据库

python - Pandas:通过 OR 条件索引行

python - 给定开始日期和结束日期,生成中间所有周的最有效方法是什么?

Python Pandas 转换列数据类型