[英]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.请帮忙。
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.