如何在 Pandas 中重置分层索引?
要在Pandas中重置分层索引,我们可以使用reset_index()方法。
步骤
创建二维、大小可变、潜在异构的表格数据df。
打印输入数据帧。
使用groupby获取不同级别的分层索引并对其进行计数。
打印多层次索引DataFrame。
使用重置多层次索引DataFrame。df.reset_index()
打印新更新的DataFrame。
示例
import pandas as pd
df = pd.DataFrame({"x": [5, 2, 1, 9], "y": [4, 1, 5, 10]})
print "Input DataFrame is:\n", df
df1 = df.groupby(["x", "y"]).count()
print "Hierarchical Index of input DataFrame is:\n", df1
df2 = df1.reset_index()
print "After resetting: \n", df2输出结果Input DataFrame is: x y 0 5 4 1 2 1 2 1 5 3 9 10 Hierarchical Index of input DataFrame is: Empty DataFrame Columns: [] Index: [(1, 5), (2, 1), (5, 4), (9, 10)] After resetting: x y 0 1 5 1 2 1 2 5 4 3 9 10
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练