使用MULTIINDEX对数据帧进行排序

时间:2017-07-26 12:23:51

标签: python pandas stack multi-index

我是一名新手,并且需要使用多索引来改变我的数据框,

我的数据框如何,请注意从csv文件中读取它。

enter image description here

我希望它看起来如何,

enter image description here

请有人帮助我,我已经这么久了。我已经尝试过堆栈功能,但它根本不能正常工作。请帮帮我..

我设法做的是:

import pandas as pd 

path =" C:\ Desktop \ data.csv" df = pd.read_csv(path,sep =&#39 ;;')

提前谢谢!

2 个答案:

答案 0 :(得分:1)

您正在寻找的函数名为.stack()。 文档:https://pandas.pydata.org/pandas-docs/stable/reshaping.html#reshaping-by-stacking-and-unstacking

一个例子:

import pandas as pd

dict_ = {
    "date": [2017,2016,2015,2014],
    "total members": [150,140,100,150],
    "lions": [20,10,5,50]
}

df = pd.DataFrame.from_dict(dict_)
table = df.set_index("date").stack() # sets date as index
table.to_frame(name='Amount').to_excel('output.xlsx') # names the column: Amount and outputs to an xlsx file

打印:

date               
2017  lions             20
      total members    150
2016  lions             10
      total members    140
2015  lions              5
      total members    100
2014  lions             50
      total members    150
dtype: int64

对原始帖子的评论

将来,您可以使用以下方式轻松共享数据框:

df.head().to_dict() # .head() return first 5 rows

此示例输出:

{'date': {0: 2017, 1: 2016, 2: 2015, 3: 2014},
 'lions': {0: 20, 1: 10, 2: 5, 3: 50},
 'total members': {0: 150, 1: 140, 2: 100, 3: 150}}

答案 1 :(得分:1)

让我们使用set_indeximport pandas as pd from io import StringIO csv_file = StringIO("""date,2017,2016,2015,2014 total members, 150, 140, 100, 150 lions, 20, 10, 5, 25 demokrats, 60, 50, 15, 25 liberals, 20, 5, 40, 25 partizans, 50, 75, 40, 25""") df = pd.read_csv(csv_file) df_out= (df.rename(columns={'date':'Parties'}) .melt(id_vars='Parties',var_name='Date',value_name='Amount') .set_index(['Date','Parties'])) print(df_out)

                    Amount
Date Parties              
2017 total members     150
     lions              20
     demokrats          60
     liberals           20
     partizans          50
2016 total members     140
     lions              10
     demokrats          50
     liberals            5
     partizans          75
2015 total members     100
     lions               5
     demokrats          15
     liberals           40
     partizans          40
2014 total members     150
     lions              25
     demokrats          25
     liberals           25
     partizans          25

输出:

safariOptions.addExtensions("..");