0

I created a dataframe where I started one dataset in ascending order and the other in descending order. But after inserting using the insert() method I get different output on two versions of Pandas 1.4.3 and 1.4.4+. In version 1.4.4+ data sorting is not saved, unlike 1.4.3.

Output version 1.4.4:

Sorted Ascending Sorted Descending
36 1 1
35 2 2
34 3 3
33 4 4
32 5 5
31 6 6
30 7 7
29 8 8
28 9 9
27 10 10

Output version 1.4.3 :

Sorted Ascending Sorted Descending
36 1 99
35 2 98
34 3 95
33 4 94
32 5 93
31 6 92
30 7 91
29 8 90
28 9 90
27 10 89

What am I doing wrong?

data = [23, 45, 12, 67, 89, 34, 56, 78, 90, 11,
                  43, 21, 76, 88, 33, 55, 77, 99, 22, 44,
                  66, 87, 32, 54, 98, 65, 87, 10, 9, 8,
                  7, 6, 5, 4, 3, 2, 1, 13, 23, 34, 45,
                  56, 67, 78, 89, 90, 91, 92, 93, 94, 95]

df = pd.DataFrame()

data_series = pd.Series(data, name='Data')

sorted_ascending = data_series.sort_values(ascending=True)
sorted_descending = data_series.sort_values(ascending=False)

df_sorted_ascending = pd.DataFrame(sorted_ascending)
df_sorted_descending = pd.DataFrame(sorted_descending)

df.insert(0, 'Sorted Ascending', df_sorted_ascending)
df.insert(1, 'Sorted Descending', df_sorted_descending)

plt.figure(figsize=(10, 6))
df.plot(kind='line')
plt.xlabel('Index')
plt.ylabel('Values')
plt.title('Sorted Data Plot')
plt.grid(True)
plt.show()

I expected to get sorted data on versions higher than 1.4.3.

user4157124
  • 2,809
  • 13
  • 27
  • 42
V1RAGE
  • 13
  • 3
  • 5
    Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Aug 26 '23 at 17:36
  • Please review #3 and **References** in [Creating Effective Stack Overflow Questions](https://trenton3983.github.io/files/Creating_Effective_StackOverflow_Questions.html) and update your question. – Trenton McKinney Aug 26 '23 at 20:35

1 Answers1

0

If you want to keep the order of the columns, you should reset the index of the ascending and the descending dataframes:

np.random.seed(42)
data = np.random.normal(0, 1, 10)

df = pd.DataFrame()

data_series = pd.Series(data, name='Data')

sorted_ascending = data_series.sort_values(ascending=True)
sorted_descending = data_series.sort_values(ascending=False)

df_sorted_ascending = pd.DataFrame(sorted_ascending)
df_sorted_descending = pd.DataFrame(sorted_descending)

# Reset index
df_sorted_ascending.reset_index(inplace=True)
df_sorted_descending.reset_index(inplace=True)

# Drop the newly created column
df_sorted_ascending.drop(columns=['index'], inplace=True)
df_sorted_descending.drop(columns=['index'], inplace=True)

df.insert(0, 'Sorted Ascending', df_sorted_ascending)
df.insert(1, 'Sorted Descending', df_sorted_descending)

plt.figure(figsize=(10, 6))
df.plot(kind='line')
plt.xlabel('Index')
plt.ylabel('Values')
plt.title('Sorted Data Plot')
plt.grid(True)
plt.show()

Output:

Sorted Ascending Sorted Descending
0 -0.469474 1.579213
1 -0.234153 1.523030
2 -0.234137 0.767435
3 -0.138264 0.647689
4 0.496714 0.542560
5 0.542560 0.496714
6 0.647689 -0.138264
7 0.767435 -0.234137
8 1.523030 -0.234153
9 1.579213 -0.469474
Yana
  • 785
  • 8
  • 23