大熊猫数据帧索引由元组?

时间:2021-06-13 23:42:39

标签: python pandas

我有一个以元组为索引的数据框:

value$: Subject<number> = new Subject<number>(99.01);

<span>{{ format(value$) }}</span> 
       <!-- R$ 99,01 -->

format(observable: BehaviorSubject<number>) {
   const frmt = new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' });
   return frmt.format(observable.getValue());
}

但是如果我像 df2 = pd.DataFrame.from_dict({"col1": [(1,2), (3,4), (5,6)], "col2": [1, 2, 3]}) df2 col1 col2 0 (1, 2) 1 1 (3, 4) 2 2 (5, 6) 3 df2.set_index("col1", inplace=True) df2 col2 col1 (1, 2) 1 (3, 4) 2 (5, 6) 3 这样的索引,它会引发任何错误。知道如何启用对象元组作为索引吗?

1 个答案:

答案 0 :(得分:0)

您需要从中创建 MultiIndex。请参阅pandas.MultiIndex.from_tuples

conda create --name tst python==2.7.12

如果您愿意,您可以删除 df2.set_index(pd.MultiIndex.from_tuples(df2['col1'].values), inplace=True) df2.loc[(1,2)] col1 (1, 2) col2 1 Name: (1, 2), dtype: object