Using list comprehension to automatically populate a list with specific data type

  dataframe, list-comprehension, pandas, python

I got a simple data in a CSV file. This data has both categorical and continuous columns. This is how I’m separating them:

df = pd.read_csv('data.csv')

categorical_columns = ['order_id', 'start_state_abr', 'end_state_abr', 'start_city', 'end_city', 'start_zip', 'end_zip']
continuous_columns = ['total_volume_inches3', 'total_distance_miles', 'total_weight_lbs']

This is too manual. It requires the programmer to read the CSV file each time there is a new column. Is there a way to populate the lists categorical_columns and continuous_columns using list comprehension?

Source: Python Questions

LEAVE A COMMENT