3

I have a pandas dataFrame with two columns that looks like the following:

d1 = {'Time1': [[93, 109, 187],[159],[94, 96, 154, 169]],
              'Time2':[[16, 48, 66, 128],[123, 136],[40,177,192]]}

df = pd.DataFrame(d1)

I need to split these columns of lists into 4 columns named 1st_half_T1, 2nd_half_T1, 1st_half_T2 and 2nd_half_T2 using pandas. The condition is, Time1 splits into 1st_half if Time <= 96 and 2nd_half if Time > 96 and applying the same condition to Time2 gives the following output.

  1st_half_T1           2nd_half_T1     1st_half_T2      2nd_half_T2
0       [93]             [109, 187]    [16, 48, 66]           [128]
1         []                  [159]              []      [123, 126]
2   [94, 96]             [154, 169]            [40]      [177, 192]

3 Answers 3

2

Use apply with a custom function

def my_split(row):
    return pd.Series({
        '1st_half_T1': [i for i in row.Time1 if i <= 96],
        '2nd_half_T1': [i for i in row.Time1 if i > 96],
        '1st_half_T2': [i for i in row.Time2 if i <= 96],
        '2nd_half_T2': [i for i in row.Time2 if i > 96]
    })
df.apply(my_split, axis=1)

Out[]:
  1st_half_T1   1st_half_T2 2nd_half_T1 2nd_half_T2
0        [93]  [16, 48, 66]  [109, 187]       [128]
1          []            []       [159]  [123, 136]
2    [94, 96]          [40]  [154, 169]  [177, 192]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all for your help.
2

Use list comprehensions with DataFrame constructor:

t11 = [[y for y in x if y <=96] for x in df['Time1']]
t12 = [[y for y in x if y >96] for x in df['Time1']]

t21 = [[y for y in x if y <=96] for x in df['Time2']]
t22 = [[y for y in x if y >96] for x in df['Time2']]

df = pd.DataFrame({'1st_half_T1':t11, '2nd_half_T1':t12,'1st_half_T2':t21, '2nd_half_T2':t22})
print (df)
  1st_half_T1 2nd_half_T1   1st_half_T2 2nd_half_T2
0        [93]  [109, 187]  [16, 48, 66]       [128]
1          []       [159]            []  [123, 136]
2    [94, 96]  [154, 169]          [40]  [177, 192]

Comments

2
df_new = pd.DataFrame()

df_new.loc[:,'1st_half_T1'] = df['Time1'].apply(lambda x : [y for y in x if y <=96])
df_new.loc[:,'2nd_half_T1'] = df['Time1'].apply(lambda x : [y for y in x if y >96])
df_new.loc[:,'1st_half_T2'] = df['Time2'].apply(lambda x : [y for y in x if y <=96])
df_new.loc[:,'2nd_half_T2'] = df['Time2'].apply(lambda x : [y for y in x if y >96])
df_new
Out[64]: 
  1st_half_T1 2nd_half_T1   1st_half_T2 2nd_half_T2
0        [93]  [109, 187]  [16, 48, 66]       [128]
1          []       [159]            []  [123, 136]
2    [94, 96]  [154, 169]          [40]  [177, 192]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.