In my table I have a multi-column index for the columns (name,folder)
I recently added a new column called date and I want to put an index into it, but I want to put it into the existing multi-column index
When I do Alter table books add index theindex (date);
I get Duplicate key name 'theindex'
How do I add another column to an index without creating a new key?
You can change the index to a different column by using set_index() after reset_index() .
pandas MultiIndex to ColumnsUse pandas DataFrame. reset_index() function to convert/transfer MultiIndex (multi-level index) indexes to columns. The default setting for the parameter is drop=False which will keep the index values as columns and set the new index to DataFrame starting from zero.
Output: Now, the dataframe has Hierarchical Indexing or multi-indexing. To revert the index of the dataframe from multi-index to a single index using the Pandas inbuilt function reset_index(). Returns: (Data Frame or None) DataFrame with the new index or None if inplace=True.
Just change the syntax to the below way to add new columns to the index:
ALTER table `books` DROP INDEX theindex;
ALTER table `books` ADD INDEX theindex (`date`, `time`);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With