版本信息:
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