I want to divide DataFrame based on the different categorical values of a column(Q14
) and name the variables separately for the resulted DataFrame. data_int.Q14 has 4 unique values (2,3,4,5). How can I create separate string variable names for DataFrames using a for loop?
Here is the image of main DataFrame (data_int)
fleet_type = data_int.Q14.unique()
for i in data_int.Q14:
for uni in fleet_type:
if i == uni:
data_'{}'.format{uni} = data_int #I tried to assign the unique values to identify the DataFrames uniquely.
File "<ipython-input-25-2200e7c4c3b7>", line 5
data_'{}'.format{uni} = data_int
^
SyntaxError: invalid syntax
Ideally, I want to use list comprehension for this particular case like below,
[data_int for i in data_int.Q14 if i == 2]
but I am not able to define the name of the DataFrame variables.
Ultimately, new DataFrame should be named as as follows,
fleet_data_list = ['fleet_type_{}'.format(i) for i in data_int.Q14.unique()]
fleet_data_list
- fleet_type_2 = (new_dataframe)
- fleet_type_3 = (new_dataframe)
- fleet_type_4 = (new_dataframe)
- fleet_type_5 = (new_dataframe)
I couldn't find a way to use fleet_data_list
to define the variable. Any idea how can I do this?