我正在使用下面的代码为数据框创建新索引。
Floating
这是当前的输出:
pd.DataFrame(pd.MultiIndex.from_product([df['Key'],pd.date_range(start='20160101', end='20160301',freq='MS')],names=['key','year_month']))
如何更改此代码,以便仅退回第一个产品? from_product是否有单独的功能或选项?
所需的输出:
0 (A, 2016-01-01 00:00:00)
1 (A, 2016-02-01 00:00:00)
2 (A, 2016-03-01 00:00:00)
3 (A, 2016-01-01 00:00:00)
4 (A, 2016-02-01 00:00:00)
5 (A, 2016-03-01 00:00:00)
6 (A, 2016-01-01 00:00:00)
7 (A, 2016-02-01 00:00:00)
8 (A, 2016-03-01 00:00:00)
9 (B, 2016-01-01 00:00:00)
10 (B, 2016-02-01 00:00:00)
11 (B, 2016-03-01 00:00:00)
12 (B, 2016-01-01 00:00:00)
13 (B, 2016-02-01 00:00:00)
14 (B, 2016-03-01 00:00:00)
15 (B, 2016-01-01 00:00:00)
16 (B, 2016-02-01 00:00:00)
17 (B, 2016-03-01 00:00:00)
答案 0 :(得分:1)
尝试使用unique
pd.DataFrame(pd.MultiIndex.from_product([df['Key'].unique(),pd.date_range(start='20160101', end='20160301',freq='MS')],names=['key','year_month']))