0

I have the following problem: For this DataFrame I used the groupby function before and saved it as a new DataFrame to find out the cumulative value of time per Unit, per Client and per Day.

So currently I have the columns: Client, Units, Time. My main goal would be to rebuild the Units column in the single column per unit to insert the values in the respective applicable column:

    Client  Units   Time    date
0    107      Q      35   2019-10-01
1    107      Q      84   2019-10-02
2    105      V      69   2019-10-03
3    107      P      10   2019-10-04
4    108      B      47   2019-10-05
5    103      V      15   2019-10-06
6    106      V      45   2019-10-07

Desired Outcome:

    Client  Q   V   P   B   date
0   107     35  0   0   0   2019-10-01
1   107     84  0   0   0   2019-10-02
2   105      0  69  0   0   2019-10-03
3   107      0  0   10  0   2019-10-04
4   108      0  0   0   47  2019-10-05
5   103      0  15  0   0   2019-10-06
6   106      0  45  0   0   2019-10-07

What I was able to deduce from some of the responses of previous posts on this topic was the following code:

pivoted = df1.pivot(index='Client', columns='Units', values='Time').reset_index()
pivoted.columns.name=None
print(pivoted)

However, I get the following error messages:

Index contains duplicate entries, cannot reshape
Data must be 1-dimensional

What is the best way to transfer it?

Many thanks in advance

jsvcgs
  • 11
  • 2
  • `df.pivot_table(index=['Client', 'date'], columns='Units', values='Time', fill_value=0).reset_index()` – mozway Nov 22 '22 at 09:06

0 Answers0