Search code examples
pythonpandasreplacerecode

Apply recode pattern to many columns


I have a dataframe with the following columns:

Name, Year, V1, V2, V5, V10, V12...

This Table contains about 40 Vx Variables. The values of these variables can be 1-5. I want to recode them so

1-3 = 0 and
4-5 = 1

I know how to replace data for one column like this

Table['V1_F'] = Table['V1'].apply(lambda x: 0 if x <4 else 1)

But I do not know how to apply this on multiple columns efficiently or is there now way around writing this replace code for each column? Best would be something like 'Do it for all columns except Name and Year.

Any help is welcome.


Solution

  • Get all columns names to variable and compare for boolean mask, then convert True/False to 1/0 by casting to integers:

    cols = Table.columns.difference(['Name','Year'])
    Table[cols] = (Table[cols] >= 4).astype(int)
    

    Or by numpy.where:

    Table[cols] = np.where(Table[cols] < 4, 0, 1)