Home > Net >  Python Pandas - Set multiple variable types based on column names
Python Pandas - Set multiple variable types based on column names

Time:11-03

I have labeled my dataset columns systematically where the suffix of categorical variables have "_c" at the end and numeric fields have "_n".

I would like python pandas code that will set the variable types based on the naming of the column headers. So for all "_c" variables I need to set them to "category" and all "_n" variables set them to "float" or "int".

Here is sample data:

fav_color_c fav_food_c income_n height_n
red pizza 100 68
blue chicken 200 70
green bbq 300 64

Can set variable types individually but having trouble to do this for a large list of variables. Any help would be greatly appreciated!

CodePudding user response:

for col in df.columns:
    if col.endswith('_c'):
        df[col]=df[col].astype(str)
    if col.endswith('_n'):
        df[col]=df[col].astype(int)
df
  • Related