Vectorized Solution To Conditional Dataframe Selection
I recently asked a question which was answered - How do I add conditionally to a selection of cells in a pandas dataframe column when the the column is a series of lists?, but I b
Solution 1:
As Edchum says, vecorised solution can be problematic.
One non vectorized solution with apply
custom functions
:
df['e'] = df['d']
def exten(lst):
return lst + [1]
def incre(lst):
lst[-1] = lst[-1] + 1return lst
df.loc[df.a != df.b, 'd'] = df.e.apply(exten)
df.loc[df.a == df.b, 'd'] = df.e.apply(incre)
df = df.drop('e', axis=1)
print df
a b c d
0OnOn [0] [0, 4]
1OnOff [0] [0, 1, 1]
2OnOn [0] [3]
3OnOn [0] [0, 4, 5]
4OnOff [0] [0, 1]
Post a Comment for "Vectorized Solution To Conditional Dataframe Selection"