熊猫数据框-根据条件添加“位置索引”

时间:2019-03-21 17:37:56

标签: python pandas dataframe

给予

df = pd.DataFrame({"LOCATION":["USA","USA","USA","USA","USA","USA","USA","JAPAN","JAPAN"],"CAR":["BMW","BMW","BMW","BMW","BMW","TOYOTA","FORD","BMW","FORD"],"SALE_DATE":[2017,2017,2017,2018,2018,2018,2019,2019,2019]})

将导致:

      CAR LOCATION  SALE_DATE
0     BMW      USA       2017
1     BMW      USA       2017
2     BMW      USA       2017
3     BMW      USA       2018
4     BMW      USA       2018
5  TOYOTA      USA       2018
6    FORD      USA       2019
7     BMW    JAPAN       2019
8    FORD    JAPAN       2019

根据年份添加“位置”的python方式是什么。换句话说,如果年份是“新的”,则从0开始直到您有了新的一年。

为使其可视化,我手动添加了column [“ position”],最终结果应如下所示:

      CAR LOCATION  SALE_DATE  POSITION
0     BMW      USA       2017       0
1     BMW      USA       2017       1
2     BMW      USA       2017       2
3     BMW      USA       2018       0
4     BMW      USA       2018       1
5  TOYOTA      USA       2018       2
6    FORD      USA       2019       0 
7     BMW    JAPAN       2019       1
8    FORD    JAPAN       2019       2

2 个答案:

答案 0 :(得分:2)

GroupBy SALE_DATE,然后使用cumcount

df['POSITION'] = df.groupby('SALE_DATE').cumcount()

    LOCATION   CAR    SALE_DATE  POSITION
0      USA     BMW       2017         0
1      USA     BMW       2017         1
2      USA     BMW       2017         2
3      USA     BMW       2018         0
4      USA     BMW       2018         1
5      USA  TOYOTA       2018         2
6      USA    FORD       2019         0
7    JAPAN     BMW       2019         1
8    JAPAN    FORD       2019         2

答案 1 :(得分:2)

使用cumcount

df['POSITION'] = df.groupby('SALE_DATE').cumcount()