简体   繁体   English

如何删除 pivot 表中的索引并将其调平

[英]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.

相关问题 如何从数据透视表中删除列名/标签,而剩余的列名下降到索引名级别? - How can remove a column name/label from a pivot table and remaining column names drop to index name level? 如何删除pandas数据透视表中的多级索引 - How to remove multilevel index in pandas pivot table 如何从pivot_table生成的数据帧的列中删除一个级别? - How to remove a level from the columns of a dataframe produced by pivot_table? 如何在索引的每个级别按agg ='sum'的总数对多索引熊猫数据框数据透视表进行排序 - How to sort a multiindex pandas dataframe pivot table by the totals of an agg='sum' at each level of the index 如何使用多索引数据框在groupby或pivot_table之后对特定级别的数据求和 - How to sum the data in specific level after groupby or pivot_table with multi-index dataframe Unpivot/Flatten pandas 数据透视表为一级索引 - Unpivot/Flatten pandas pivot table into one level index 如何创建具有多级列的数据透视表? - How to create a pivot table with multiple level columns? 如何避免按索引排​​序数据透视表 - How to avoid sorting pivot table by index 如何使用索引中的行来制作数据透视表 - How to make a pivot table with values in rows by index 如何在 pandas 中创建具有多个索引的 pivot 表? - how to create the pivot table in pandas with multiple index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM