1

I have this data as dataframe and I want to group dates and create columns of time :

        empIdn      date      time
36       517     2019-05-02  12:00:12
54       517    2019-05-02  12:32:05
223      517    2019-05-02  05:18:57
95       517    2019-05-02  05:23:02

and what I want to achieve is

      empIdn       date      timeA    timeB     timeC      timeD
36     517     2019-05-02  12:00:12    12:32:05  05:18:57   05:23:02

Here is what I've tried so far:

datas = pd.read_csv(datFile, sep="\t",usecols=[0, skipinitialspace=True, names=['empId', 'date_time'])
df = pd.DataFrame(data = datas)
new = df['date_time'].str.split(" ", n = 1, expand = True)

df['empIdn'] =  df['empId'].astype(int)
df['date'] = new[0]
df['time'] = new[1]
df['date'] = pd.to_datetime(df['date'])
df['time'] = pd.to_datetime(df['time'],format= '%H:%M:%S' ).dt.time

df.drop(columns=['date_time'], inplace = True)
df.drop(columns=['empId'], inplace = True)
df.sort_values('empIdn',inplace = True, ascending = True)
N. Arunoprayoch
  • 922
  • 12
  • 20

1 Answers1

0
df3=df.groupby(['empIdn','date'])['time'].agg(list).reset_index(name='new')
df3 = df3.join(pd.DataFrame(df3.new.values.tolist(), df3.index).add_prefix('time_'))
df3.drop('new', axis=1, inplace = True)
moys
  • 7,747
  • 2
  • 11
  • 42