0

I'm have some hard to convert this table below to a single row.

enter image description here

The table I want to achieve is this one:

enter image description here

So far, I was able to get this with the code below.

enter image description here

df_hours = df.pivot(columns='DayID', values='StartTime')

But, as you can see, the SiteID remains in all rows. What I need now, is to keep it in a single row only. Any ideas?

bellotto
  • 445
  • 3
  • 13

1 Answers1

0

As the DayID column is not unique... so try this;

df1["key"] = range(0,df1.shape[0])
df1["DayID"] = df1["DayID"].astype(str) + df1["key"].astype(str)
df2 = df1.pivot_table(index='SiteID', columns='DayID', values='StartTime')
df2.columns = ["".join(x for x in str1 if x.isalpha()) + "StartTime" for str1 in df2.columns]

Hope this Helps...

Sachin Kohli
  • 1,956
  • 1
  • 1
  • 6