Skip to content Skip to sidebar Skip to footer

While Loop Alternative In Python

I am working on a huge dataframe and trying to create a new column, based on a condition in another column. Right now, I have a big while-loop and this calculation takes too much t

Solution 1:

Don't use loops in pandas, they are slow compared to a vectorized solution - convert boolean mask to integers by astypeTrue, False are converted to 1, 0:

dataframe = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':list('aaabbb'),
                   'F':[5,3,6,9,2,4],
                   'G':[5,3,6,9,2,4]
})

a = 5
dataframe['new'] = (dataframe.iloc[:,5] >= a).astype(int)
print (dataframe)
   A  B  C  D  E  F  G  new0  a  471  a  5511  b  583  a  3302  c  495  a  6613  d  547  b  9914  e  521  b  2205  f  430  b  440

If you want to overwrite the 7th column:

a = 5
dataframe.iloc[:,6] = (dataframe.iloc[:,5] >= a).astype(int)
print (dataframe)
   A  B  C  D  E  F  G
0  a  471  a  511  b  583  a  302  c  495  a  613  d  547  b  914  e  521  b  205  f  430  b  40

Post a Comment for "While Loop Alternative In Python"