I have the following pandas DataFrame with mixed data types: string and integer values. I want to sort values of this DataFrame in descending order using multiple columns: Price
and Name
. The string values (i.e. Name
) should be sorted in the alphabetical order, or actually can be ignored at all, because the most important ones are numerical values.
The problem is that the list of target columns can contain both string and integer columns, e.g. target_columns = ["Price","Name"]
d = {'1': ['25', 'AAA', 2], '2': ['30', 'BBB', 3], '3': ['5', 'CCC', 2], \
'4': ['300', 'DDD', 2], '5': ['30', 'DDD', 3], '6': ['100', 'AAA', 3]}
columns=['Price', 'Name', 'Class']
target_columns = ['Price', 'Name']
order_per_cols = [False] * len(target_columns)
df = pd.DataFrame.from_dict(data=d, orient='index')
df.columns = columns
df.sort_values(list(target_columns), ascending=order_per_cols, inplace=True)
Currently, this code fails with the following message:
TypeError: '<' not supported between instances of 'str' and 'int'
The expected output:
Price Name Class
300 DDD 2
100 AAA 3
30 DDD 3
30 BBB 3
25 AAA 2
5 CCC 2
Copyright Notice:Content Author:「Fluxy」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59164941/how-to-sort-values-in-dataframe-for-both-numerical-and-string-values