[英]How to remove a index in pivot table and level it
import numpy as np
import pandas as pd
import seaborn as sns
Here I bring sns flights detail and try to remove index to do a correlation.在这里,我带来了 sns 航班的详细信息,并尝试删除索引来进行关联。 I first need to do a make dataframe looks like the last data frame in here
我首先需要做一个使 dataframe 看起来像这里的最后一个数据帧
flights = sns.load_dataset("flights")
flights.head()
flights = flights.pivot( "month","year", "passengers")
flights.reset_index(inplace=True)
flights.head()
year month 1949 1950 1951 1952 1953 1954 1955
0 January 112 115 145 171 196 204 242
1 February 118 126 150 180 196 188 233
2 March 132 141 178 193 236 235 267
3 April 129 135 163 181 235 227 269
4 May 121 125 172 183 229 234 270
I basically want to remove year in here and keep the dataframe as below without any level我基本上想在这里删除一年并保持dataframe如下没有任何级别
month 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
January 112 115 145 171 196 204 242 284 315 340 360 417
February 118 126 150 180 196 188 233 277 301 318 342 391
March 132 141 178 193 236 235 267 317 356 362 406 419
April 129 135 163 181 235 227 269 313 348 348 396 461
May 121 125 172 183 229 234 270 318 355 363 420 472
You can change name of the columns and reset_index
as:您可以将列名称和
reset_index
为:
flights.columns.name = None
flights = flights.reset_index()
print(flights.iloc[:,:5])
month 1949 1950 1951 1952
0 January 112 115 145 171
1 February 118 126 150 180
2 March 132 141 178 193
3 April 129 135 163 181
4 May 121 125 172 183
5 June 135 149 178 218
6 July 148 170 199 230
7 August 148 170 199 242
8 September 136 158 184 209
9 October 119 133 162 191
10 November 104 114 146 172
11 December 118 140 166 194
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.