0

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!

1
  • What have you tried so far? Any ideas you came up with yourself? Commented Nov 2, 2022 at 15:01

1 Answer 1

1
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.