As part of an assignment, Im trying to reorganize the columns in a way that the types (of coffee) are in alphabetical order. I implemented the following code, and it does work:
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
BeanFileCleaned = BeanFileCleaned.reindex(columns=Reorganized)
BeanFileCleaned
However, when I run the file again, all the values in the table turn Nan and for some reason, the rows also have the same name as the columns (there are a lot of rows, so this shouldnt happen).
How do I fix this? Any help is appreciated, thanks in advance.
Edit: Forgot to add, this is what the result looks like:
Assuming that BeanFileCleaned is a pandas dataframe, you are probably looking for something like this.
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
Reorganized = sorted(Reogranized)
BeanFileCleaned = BeanFileCleaned[Reorganized]
If you have extra columns you want to append to the end you can do it like
all_columns = BeanFileCleaned.columns
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
Reorganized = sorted(Reogranized)
Reorganized_set = set(Reorganized)
rest_columns = [column for column in all_columns if column not in Reorganized_set]
BeanFileCleaned = BeanFileCleaned[Reorganized + rest_columns]