您的位置: 网站首页> Pandas教程> 当前文章
pandas创建多重索引的7种方法
老董-我爱我家房产SEO2022-08-14165围观,145赞
pandas创建多重索引有很多方式,老董在使用过程中一共总结了7种类方式,本文为大家总结1下。
1、pd.MultiIndex.from_arrays():
可以直接传入二维列表作为参数。
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd arrays = [['baidu', 'baidu', 'sogou', 'sogou'], ['pc', 'wap', 'pc', 'wap',]] index= pd.MultiIndex.from_arrays(arrays, names=('name', 'pingtai')) print(index) df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[11,22,33,44],[55,66,77,88]],index=index) print(df)
MultiIndex([('baidu', 'pc'), ('baidu', 'wap'), ('sogou', 'pc'), ('sogou', 'wap')], names=['name', 'pingtai']) 0 1 2 3 name pingtai baidu pc 1 2 3 4 wap 5 6 7 8 sogou pc 11 22 33 44 wap 55 66 77 88
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd arrays = [['baidu', 'baidu', 'sogou', 'sogou'], ['pc', 'wap', 'pc', 'wap',]] index= pd.MultiIndex.from_arrays(arrays, names=('name', 'pingtai')) print(index) df = pd.DataFrame([[1,2,3,4]],columns=index) print(df)
MultiIndex([('baidu', 'pc'), ('baidu', 'wap'), ('sogou', 'pc'), ('sogou', 'wap')], names=['name', 'pingtai']) name baidu sogou pingtai pc wap pc wap 0 1 2 3 4
2、pd.MultiIndex.from_tuples():
可以直接传入元组组成的的列表作为参数。
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd tuples = [('baidu', 'pc'), ('baidu', 'wap'), ('sogou', 'pc'), ('sogou', 'wap')] index= pd.MultiIndex.from_tuples(tuples, names=('name', 'pingtai')) df1 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[11,22,33,44],[55,66,77,88]],index=index) df2 = pd.DataFrame([[1,2,3,4]],columns=index) # 自己输出验证把
3、pd.MultiIndex.from_product():
可迭代对象组成的列表作为参数,列表内每个可迭代对象的元素的笛卡尔积创建索引。
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd tuples = [['baidu', 'sogou'], ['pc', 'wap']] index= pd.MultiIndex.from_product(tuples, names=('name', 'pingtai')) df1 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[11,22,33,44],[55,66,77,88]],index=index) df2 = pd.DataFrame([[1,2,3,4]],columns=index) # 自己输出验证把
4、pd.MultiIndex.from_frame:
传入1个df,根据df数据框(注意不是索引)作为多重索引创建df
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd tuples = [['baidu', 'sogou'], ['pc', 'wap']] index= pd.MultiIndex.from_product(tuples, names=('name', 'pingtai')) df2 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[11,22,33,44],[55,66,77,88]],index=index) print(df2) print('------------------') # 自己输出验证把 index = pd.MultiIndex.from_frame(df2) print(index)
0 1 2 3 name pingtai baidu pc 1 2 3 4 wap 5 6 7 8 sogou pc 11 22 33 44 wap 55 66 77 88 ------------------ MultiIndex([( 1, 2, 3, 4), ( 5, 6, 7, 8), (11, 22, 33, 44), (55, 66, 77, 88)], names=[0, 1, 2, 3])
5、groupby():
通过分组统计得到多重索引,groupby函数默认as_index=True,即把分组的列作为索引
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd df = pd.DataFrame({'name': ['baidu', 'baidu','sogou', 'sogou'], 'pingtai':['pc','wap','pc','wap'], 'value': [100., 200., 50., 80.]} ) df_res = df.groupby(['name','pingtai']).sum() print(df_res) print('----------------') print(df_res.index)
value name pingtai baidu pc 100.0 wap 200.0 sogou pc 50.0 wap 80.0 ---------------- MultiIndex([('baidu', 'pc'), ('baidu', 'wap'), ('sogou', 'pc'), ('sogou', 'wap')], names=['name', 'pingtai'])
6、pivot_table():
pivot_table函数生成透视表(其实也是分组聚合)
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd import numpy as np df = pd.DataFrame({"A": ["baidu", "baidu", "baidu", "baidu", "baidu", "google", "google", "google", "google"], "B": ["pc", "pc", "pc", "wap", "wap", "pc", "pc", "wap", "wap"], "C": [1, 2, 2, 3, 3, 4, 5, 6, 7], "D": [2, 4, 5, 5, 6, 6, 8, 9, 9]}) table = pd.pivot_table(df, values='D', index=['A', 'B'],aggfunc=np.sum) print(table) print('----------------') print(table.index)
D A B baidu pc 11 wap 11 google pc 14 wap 18 ---------------- MultiIndex([( 'baidu', 'pc'), ( 'baidu', 'wap'), ('google', 'pc'), ('google', 'wap')], names=['A', 'B'])
7、读取excel文件
读取excel文件,通过inde_col或者header参数设置多重索引
# ‐*‐ coding: utf‐8 ‐*‐ import pandas as pd import numpy as np # 第1和2列设为行索引 pd.read_excel(file_excel,index_col=[0,1]) # 第1和2行设为列索引 pd.read_excel(file_excel,header=[0,1])
很赞哦!
python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群
相关文章
文章评论
-
pandas创建多重索引的7种方法文章写得不错,值得赞赏
站点信息
- 网站程序:Laravel
- 客服微信:a772483200