我有一个约20k行的数据帧,看起来是这样的:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Car_ID': ['B332', 'B332', 'B332', 'C315', 'C315', 'C315', 'C315', 'C315', 'F310', 'F310'], \
'Date': ['2018-03-12', '2018-03-14', '2018-03-15', '2018-03-17', '2018-03-13', '2018-03-15', \
'2018-03-18', '2018-03-21', '2018-03-10', '2018-03-13'], \
'Driver': ['Alex', 'Alex', 'Mick', 'Sara', 'Sara', 'Jean', 'Sara', 'Sara', 'Franck','Michel']})
df
Out:
Car_ID Date Driver
0 B332 2018-03-12 Alex
1 B332 2018-03-14 Alex
2 B332 2018-03-15 Mick
3 C315 2018-03-17 Sara
4 C315 2018-03-13 Sara
5 C315 2018-03-15 Jean
6 C315 2018-03-18 Sara
7 C315 2018-03-21 Sara
8 F310 2018-03-10 Franck
9 F310 2018-03-13 Michel
我为数据帧中的每个事件创建一个新列,如下所示:
df["Event"] = np.where(df.Car_ID.str.contains('B', case=True, na=False), 'Rent_Car_B', \
np.where(df.Car_ID.str.contains('C', case=True, na=False), 'Rent_Car_C', \
np.where(df.Car_ID.str.contains('F', case=True, na=False), 'Rent_Car_F', df.Car_ID)))
df
Out:
Car_ID Date Driver Event
0 B332 2018-03-12 Alex Rent_Car_B
1 B332 2018-03-14 Alex Rent_Car_B
2 B332 2018-03-15 Mick Rent_Car_B
3 C315 2018-03-17 Sara Rent_Car_C
4 C315 2018-03-13 Sara Rent_Car_C
5 C315 2018-03-15 Jean Rent_Car_C
6 C315 2018-03-18 Sara Rent_Car_C
7 C315 2018-03-21 Sara Rent_Car_C
8 F310 2018-03-10 Franck Rent_Car_F
9 F310 2018-03-13 Michel Rent_Car_F
对于我的Event
列,我想为每个驱动程序添加新行,如下所示:
Out:
Car_ID Date Driver Event
0 B332 2018-03-12 Alex Rent_Car_B
1 B332 2018-03-14 Alex Rent_Car_B
2 B332 2018-03-15 Mick Rent_Car_B
3 B332 2018-03-15 Alex to Mick
4 C315 2018-03-17 Sara Rent_Car_C
5 C315 2018-03-13 Sara Rent_Car_C
6 C315 2018-03-15 Jean Rent_Car_C
7 C315 2018-03-15 Sara to Jean
8 C315 2018-03-18 Sara Rent_Car_C
9 C315 2018-03-18 Jean to Sara
10 C315 2018-03-21 Sara Rent_Car_C
11 F310 2018-03-10 Franck Rent_Car_F
12 F310 2018-03-13 Michel Rent_Car_F
13 F310 2018-03-13 Franck to Mike
我不确定是否有什么诀窍来实现这项工作。 我将不胜感激你的忠告!你知道吗
这是一个相当复杂的问题,我的观点是:
如果你想要顺序,那么对索引进行排序
你只需要几次换班就可以很好地做到这一点!您还可以使用此方法正确地获取索引,并将它们精确地添加到所需的位置。你知道吗
添加
Event
后,执行DataFrame
:输出:
使用
shift
方法,首先用它创建一个列,我们将在下面使用它:选择您实际更换驾驶员和带面罩的相同车号的行:
现在,通过添加0.5来更改索引,以便以后进行连接和排序,并更改两列的值:
现在可以连接、排序、重置索引并删除:
你会得到:
编辑:在每个驱动程序和日期前添加一行
结果是df2
相关问题 更多 >
编程相关推荐