While Loop Alternative in Python

2019-07-16 07:49发布

问题:

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 time, is there an easier way to do it?

With lambda for example?:

def promo(dataframe, a):  
    i=0
    while i < len(dataframe)-1:
        i=i+1
        if dataframe.iloc[i-1,5] >= a:
            dataframe.iloc[i-1,6] = 1
        else:
            dataframe.iloc[i-1,6] = 0

    return dataframe

回答1:

Don't use loops in pandas, they are slow compared to a vectorized solution - convert boolean mask to integers by astype True, 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  new
0  a  4  7  1  a  5  5    1
1  b  5  8  3  a  3  3    0
2  c  4  9  5  a  6  6    1
3  d  5  4  7  b  9  9    1
4  e  5  2  1  b  2  2    0
5  f  4  3  0  b  4  4    0

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  4  7  1  a  5  1
1  b  5  8  3  a  3  0
2  c  4  9  5  a  6  1
3  d  5  4  7  b  9  1
4  e  5  2  1  b  2  0
5  f  4  3  0  b  4  0