简体   繁体   English

在熊猫数据框中创建多索引列

[英]Make multiindex columns in a pandas dataframe

I have a pandas dataframe with the following strcuture: 我有一个具有以下结构的熊猫数据框:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(32).reshape((4,8)), 
            index = pd.date_range('2016-01-01', periods=4),
            columns=['male ; 0', 'male ; 1','male ; 2','male ; 4','female ; 0','female ; 1','female ; 2','female ; 3',])

The column names are messy with a combination of two variable in the header name, and residual punctuation from the original spreadsheet. 列名称杂乱无章,标题名称中包含两个变量以及原始电子表格中的残留标点符号。

What I want to do is set a column MultiIndex called sex and age in my dataframe. 我想做的是在我的数据框中设置一个名为“性别和年龄”的MultiIndex列。

I tried using pd.MultiIndex.from_tuples like this: 我试过像这样使用pd.MultiIndex.from_tuples

columns = [('Male', 0),('Male', 1),('Male', 2),('Male', 3),('Female', 0),('Female', 1),('Female', 2),('Female', 3)]
df.columns = pd.MultiIndex.from_tuples(columns)

And then naming the column indexes: 然后命名列索引:

df.columns.names = ['Sex', 'Age']

This gives the result that I would like. 这给出了我想要的结果。 However, my dataframes has ages to over 100 for each sex so this is not very practical. 但是,我的数据框对于每个性别的年龄都超过100岁,因此这不是很实际。

Could someone please guide me on how to set MultiIndex columns from a tuple programatically. 有人可以指导我如何以编程方式从元组设置MultiIndex列。

Jaco's answer works nicely, but you can even create a MultiIndex from a product directly using .from_product() : Jaco的答案很好用,但是您甚至可以直接使用.from_product()从产品创建MultiIndex

sex = ['Male', 'Female']
age = range(100)
df.columns = pd.MultiIndex.from_product([sex, age], names=['Sex', 'Age'])

You can use the itertools module to generate your columns variable by taking the cartesian join of gender and the age range in your data, for example: 您可以使用itertools模块,通过在数据中采用性别和年龄范围的笛卡尔连接来生成columns变量,例如:

import itertools
max_age = 100
sex = ['Male','Female']
age = range(max_age)
columns=list(itertools.product(sex, age))
df.columns = pd.MultiIndex.from_tuples(columns)
df.columns.names = ['Sex', 'Age']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM