0

I have a dataframe with the part numbers associated with specific auto vehicles. I am trying to use a for loop to break the larger dataframe (with all vehicles in it) into smaller dataframes for each vehicle. My main issue is that I need to reference the vehicle names and place them in the name for their respective dataframe. For example, I need all Altima parts numbers to be in a dataframe called df2020 Altima

I am using the below code to accomplish this

df4_vehicle_list = df4['Model_Name'].unique()
df4_vehicle_list

This code returns: array(['2020 Maxima', '2020 Altima', '2020 Leaf', '2019 Altima', '2020(.5) Rogue', '2020 JX', '2021 Murano', '2019 Pathfinder', '2020 Pathfinder', '2021 Rogue'], dtype=object)

Then I try to use the below code

for i in df4_vehicle_list:
    name = df4['Model_Name'] == i
    df[i] = df4[name]

Joey
  • 1

1 Answers1

1

IIUC, this is what you're looking for. The == tests for equality, while = is used for assignment. So the first row after the for line creates a df slice of each model model. The next line assigns the value of your iteration variable i to the Model Name column.

df_list = []

for i in df4_vehicle_list:
    df = df4.loc[df4['Model_Name'] == i]
    df['Model_Name'] = i

    df_list.append(df)

concatted_df = pd.concat(df_list)
Matthew Borish
  • 3,016
  • 2
  • 13
  • 25
  • This code didn't quite work. I was left only 2021 Rogue data because the code wrote over df with each iteration of i. I was looking to get the "Model_Name" in the name of the dataframe. Also, I thought i would need to remove the spaces in the "Model_Name" to be able to reference the df (df names don't like spaces). – Joey Jun 30 '20 at 18:08
  • My original answer was just for the assignment part of the problem. I edited it to avoid the overwriting issue like you described, but it's hard to confirm everything is working without sample data. Can you add a small amount of sample data? I will make sure this solution works for you if you do. – Matthew Borish Jun 30 '20 at 22:11