Sorting Grouped Cells in Excel Based on One Column: Alphanumerical Values
In this article, we will discuss how to sort grouped cells in Excel based on one column with alphanumerical values. This is a common task when working with datasets that have multiple recommendations, design features, or changes, each with multiple attributes. In our example, we will use a dataset with one row representing one street and three sets of columns representing recommendation design features and changes (like adding trees).
Context
Suppose we have a dataset with the following structure:
| Street Name | Design Feature 1 - Change 1 | Design Feature 1 - Change 2 | Design Feature 1 - Change 3 | Design Feature 2 - Change 1 | Design Feature 2 - Change 2 | Design Feature 2 - Change 3 | | --- | --- | --- | --- | --- | --- | --- | | Main St | Add trees | Remove benches | Install lighting | Paint buildings | Add planters | Remove graffiti | | Oak St | Add benches | Install lighting | Paint buildings | Remove graffiti | Add trees | Add planters | | Elm St | Install lighting | Remove benches | Add trees | Add planters | Paint buildings | Remove graffiti |
Our goal is to sort the grouped cells based on the values in the first column (Design Feature 1 - Change 1) in alphanumerical order.
Procedure
-
Convert the dataset into a table:
To make it easier to work with the dataset, convert it into a table by selecting any cell within the dataset and pressing
Ctrl + T. -
Ungroup the cells:
To ungroup the cells, select any cell within the table and press
Ctrl + Shift + Right Arrowto select all the cells in the row. Then, pressCtrl + Shift + Down Arrowto select all the rows in the table. Finally, pressCtrl + Spaceto select all the columns in the table, and then pressCtrl + Shift + Lto ungroup the cells. -
Convert the first column into a list:
Select the first column (Design Feature 1 - Change 1) and press
Ctrl + Cto copy the data. Then, right-click on any cell below the table and selectPaste Special > Values. This will convert the column into a list. -
Sort the list in alphanumerical order:
Select the list and go to the
Datatab in the Excel ribbon. Click onSort & Filterand selectA to Zto sort the list in alphanumerical order. -
Replace the sorted list with the original data:
Cut and paste the sorted list back into the first column of the table.
-
Regroup the cells:
Select all the cells in the table and press
Ctrl + Spaceto select all the columns. Then, pressCtrl + Shift + Lto regroup the cells. -
Format the table:
You can now format the table as desired using Excel's formatting tools.
Code Block
Here's an example of how to sort a similar dataset using Python and the pandas library:
import pandas as pd
# Create a DataFrame from the dataset
df = pd.DataFrame({
'Street Name': ['Main St', 'Oak St', 'Elm St'],
'Design Feature 1 - Change 1': ['Add trees', 'Add benches', 'Install lighting'],
'Design Feature 1 - Change 2': ['Remove benches', 'Install lighting', 'Remove benches'],
'Design Feature 1 - Change 3': ['Install lighting', 'Paint buildings', 'Add trees'],
'Design Feature 2 - Change 1': ['Paint buildings', 'Remove graffiti', 'Add planters'],
'Design Feature 2 - Change 2': ['Add planters', 'Add trees', 'Paint buildings'],
'Design Feature 2 - Change 3': ['Remove graffiti', 'Add planters', 'Remove graffiti']
})
# Ungroup the cells
df = df.melt(id_vars=['Street Name'], var_name='Design Feature', value_name='Change')
# Sort the grouped cells in alphanumerical order
df = df.sort_values(by=['Design Feature', 'Change'])
# Regroup the cells
df = df.pivot(index='Street Name', columns='Design Feature', values='Change')
# Format the table
df.style.set_table_styles([
{'selector': 'th', 'props': [('text-align', 'center')]},
{'selector': 'td', 'props': [('text-align', 'left')]}
])
Summary
In this article, we discussed how to sort grouped cells in Excel based on one column with alphanumerical values. We provided a step-by-step procedure for sorting the grouped cells and provided an example using Python and the pandas library.
References
- Excel: Sort data in a table or range
- Python: pandas library documentation