简体   繁体   English

如何使用数据框创建层次结构

[英]How to create a hierarchical structure with dataframe

I am looking for a way to make it dynamic because the range of userlevels is wide.我正在寻找一种使其动态化的方法,因为用户级别的范围很广。

Please help.请帮忙。

enter image description here在此处输入图片说明

Since the intent of the question is not clear, if you want to create a hierarchical index, you can achieve this in the following way由于问题的意图不明确,如果你想创建一个分层索引,你可以通过以下方式来实现

import pandas as pd

idx = pd.MultiIndex.from_product(([21,22],[1,2,3,4,5],[60,70,80,90,100]), names=["uselevel","class","upper"])
pd.DataFrame(index=idx, columns=['column1','column2','column3'])

df.head(10)
            column1 column2 column3
uselevel    class   upper           
21  1   60  NaN NaN NaN
        70  NaN NaN NaN
        80  NaN NaN NaN
        90  NaN NaN NaN
       100  NaN NaN NaN
    2   60  NaN NaN NaN
        70  NaN NaN NaN
        80  NaN NaN NaN
        90  NaN NaN NaN
       100  NaN NaN NaN

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

相关问题 如何从现有的层次结构和任意形状的 pandas dataframe 创建父子 dataframe? - How to create a parent-child dataframe from an existing pandas dataframe of hierarchical structure and arbitrary shape? 如何按层次类别结构中的值对 pandas 中的 dataframe 进行排序 - How to sort dataframe in pandas by value in hierarchical category structure 如何从 Python 中的分层数据创建树结构? - How to create tree structure from hierarchical data in Python? 熊猫数据框列到分层数据结构? - pandas dataframe columns to hierarchical data structure? 如何通过python中的dataframe中的id列创建层次路径? - How to create hierarchical path by id column in a dataframe in python? 如何从 spark dataframe 的层次关系创建字典 - How to create dictionary from hierarchical relations from spark dataframe 分层Pandas:如何从两个数据帧的列创建分层pandas数据帧? - Hierarchical Pandas: How to create hierarchical pandas dataframe from columns of two dataframes? 如何有效地解压缩这种类型的结构并创建一个DataFrame? - How to effectively unpack this type of structure and create a DataFrame? 如何从具有复杂结构的列表中创建 DataFrame? - How to create a DataFrame from a list with complex structure? 如何从下面的结构创建 dataframe - How to create dataframe from below structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM