0

I have several excel data files, each one referring to a different timing (i.e. 0h, 24h, 48h, ...), and the columns with the data of interest are named: 'Product' and 'Value'. I have concatenated those files by using the following:

result = pd.concat([pd.read_excel(file) for file in filenames], keys=t_list, names=['t'])

where filenames is a list containing the excel files, t_list is a list containing the timings, 't' is the name of the newly created column with the timings. So far so good, and I get a new dataframe with the following structure:

Concatenated Dataframe:
concatenated dataframe

But then, if I sort with the following:

result['Product'].astype(str)
result.sort_values('Product', ascending=True)

I found the correct order for some items, but not for others. Indeed, I get something like the following:

Dataframe not properly sorted:
dataframe not properly sorted

Can someone shed a light on this?

2
  • you need to specify the columns for the sort you're dealing with unstructured data with no proper index. Commented Dec 30, 2020 at 11:41
  • Thanks a lot Manakin, but the answer by Daweo (see below) solved this issue Commented Dec 30, 2020 at 11:56

2 Answers 2

1

Can someone shed a light on this?

Default sorting algorithm of sort_values is not stable, therefore order of elements with equal Product might be different than before sorting. To avoid that you might elect to use kind="mergesort" in sort_values.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of passing 'Products', pass a list of the columns you want to sort by.

If you want to sort by 'Product' and then by 'Value', it looks like this:

result.sort_values(['Product', 'Value'], ascending=True)

1 Comment

Thanks for your answer, but if I try to sort by 'Product' and 't' (this is what I need), then I get the following error message: ValueError: No axis named t for object type DataFrame

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.