-3

I want to groupby my data using a column (No) and keep each result of the columns date1 and results in different columns.

Here is an example of an input with the corresponding expected output :

enter image description here

I've added a little more data. and There's a lot of data.

tomy
  • 55
  • 6
  • 1
    Please present your data as data and not as an image. see: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Itamar Mushkin Jul 29 '19 at 08:01
  • There is more data available,and fixed code....... df = pd.DataFrame({'No.' : df['date_1'], 'date_1' : [datetime.now() for x in range(3)], 'results' : df['results']}) – tomy Jul 29 '19 at 09:20
  • Then add that more data and fixed code to the question body, not in a comment – Itamar Mushkin Jul 29 '19 at 09:26
  • 1
    ...Why would a question be put on hold as unclear after two users already understood and answered it? – Itamar Mushkin Jul 29 '19 at 10:06

2 Answers2

2

Here is a way to do it :

from datetime import datetime

df = pd.DataFrame({'No.' : ['s1', 's2', 's2'], 'date_1' : [datetime.now() for x in range(3)],
                  'results' : [1.2, 9.73, 3.71]})

# Use groupby to get the lists of dates and result
result = df.groupby('No.')[['date_1', 'results']].agg({'date_1' : list, 'results' : list})
# if you are running a pandas version <0.24.2 uncomment the following line and comment the one above
#result = df.groupby('No.')[['date_1', 'results']].agg({'date_1' : lambda x: list(x), 'results' : lambda x: list(x)})

# Look at the number of columns we will have to create
len_max = np.max([len(x) for x in result['results']])

# Create all the required columns  
for i in range(1,len_max):
    result['date__{}'.format(i+1)] = [x[i] if len(x)>i else 0 for x in result['date_1']]
    result['results_{}'.format(i+1)] = [x[i] if len(x)>i else 0 for x in result['results']]

# Modify the first  two columns that still contain the lists of the groupby
result['date_1'] = [x[0] for x in result['date_1']]
result['results'] = [x[0] for x in result['results']]

Output :

                        date_1  results                     date__2  results_2
No.                                                                           
s1  2019-07-29 08:00:45.878494     1.20                           0       0.00
s2  2019-07-29 08:00:45.878499     9.73  2019-07-29 08:00:45.878500       3.71
vlemaistre
  • 3,301
  • 13
  • 30
1

Building upon vlemaistre's answer - you can do it in a more compact way:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta
df = pd.DataFrame({'No.' : ['s1', 's2', 's2'], 'date' : [datetime.now()+timedelta(days=x) for x in range(3)],
                  'results' : [1.2, 9.73, 3.71]})

joint_df = df.groupby('No.')[['date', 'results']].agg(lambda x: list(x))
result = pd.DataFrame(index=joint_df.index)
for column in df.columns.difference({'No.'}):
    result = result.join(pd.DataFrame.from_records(
        list(joint_df[column]), index=joint_df.index).rename(lambda x: column+str(x+1), axis=1), how='outer')

Output is:

    date1                       date2                       results1    results2
No.             
s1  2019-07-29 12:58:28.627950  NaT                         1.20        NaN
s2  2019-07-30 12:58:28.627957  2019-07-31 12:58:28.627960  9.73        3.71
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32