我有一个2索引级别的dataframe: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 我想把这个变成: Trial measurement value 1 0 13 1 1 3 1 2 4 2 0 NaN 2 1 12 3 0 34 我怎么能最好的做到这一点? 我需要这个,因为我想按照这里的指示来聚合数据,但是如果它们被用作索引,我不能select这样的列。
我从这样的input数据开始 df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , "City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"] } ) 当打印时显示为: City Name 0 Seattle Alice 1 Seattle Bob 2 Portland Mallory 3 Seattle Mallory 4 Seattle Bob 5 Portland Mallory 分组非常简单: g1 = df1.groupby( [ "Name", "City"] ).count() 并打印产生一个GroupBy对象: […]