import pandas as pd
df = pd.DataFrame({'Records': ['Name: hours on date, Name: hours on date',
'Aya: 20 on 18/9/2021, Asmaa: 10 on 20/9/2021, Aya: 20 on 20/9/2021']})
# Keep only rows that have the actual data
data = df.loc[~df['Records'].str.contains('Name')]
# Split on the comma delimiter and explode into a unique row per employee
data = data['Records'].str.split(',').explode()
# Use regex to capture the relevant data and construct the dictionary
data = data.str.extract('([a-zA-z]+)\:\s(\d{1,2})\son\s(\d{1,2}\/\d{1,2}\/\d{4})').reset_index(drop=True)
data.groupby(2).apply(lambda x: dict(zip(x[0],x[1]))).to_dict()
您可以在这里使用dict,但它必须嵌套,因为每个日期都有多个条目
输出
相关问题 更多 >
编程相关推荐