Search code examples
rapidscudf

Convert cuDF data frame column to 1 or 0 for “true”/“false” values


I am using RAPIDS (0.9 release) docker container. How can I do the following with RAPIDS cuDF?

df['new_column'] = df['column_name'] > condition df[['new_column']] *= 1


Solution

  • You can do this in the same way as with pandas.

    import cudf
    
    df = cudf.DataFrame({'a':[0,1,2,3,4]})
    df['new'] = df['a'] >= 3
    df['new'] = df['new'].astype('int') # could use int8, int32, or int64
    
    # could also do (df['a'] >= 3).astype('int')
    df
    
        a   new
    0   0   0
    1   1   0
    2   2   0
    3   3   1
    4   4   1