Home > Software design >  Python - Re-Order CSV columns to match DataFrame
Python - Re-Order CSV columns to match DataFrame

Time:11-06

Assume I have a table/dataframe with the following columns:

enter image description here

Now, I have a csv file that has values that can be added to this table, but not in the right order:

enter image description here

Is there a way to order the columns in the csv to match the format of the Table/Dataframe? (Assume this applies for many columns/rows of data.)

CodePudding user response:

You can reordre the columns using select() method:

df.select("Fruit","Color", "Taste")

Also if you want to union the 2 dataframes you can use unionByName without changing the order of columns like this:

df1.unionByName(df2)

CodePudding user response:

Assuming both dataframes have the same columns, you can try this:

df2 = df2[df1.columns]

Or:

df2 = df2.reindex(df1.columns, axis=1)
  • Related