6

I have a following code of snippet.

df = pd.DataFrame({'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'],
                   'col2' : [2, 1, 9, 8, 7, 4],
                   'col3': [0, 1, 9, 4, 2, 3]})
print(df)
sorted=df.sort_values(by=1,axis=1)
print(sorted)

enter image description here

The above data is original dataframe .

enter image description here

The above one is output of the df.sort_values() function.

Can anyone explain what is happening here?

ak_slick
  • 1,006
  • 6
  • 19
Ravi
  • 2,778
  • 2
  • 20
  • 32
  • 2
    What do pandas docs say regarding this? – taras Aug 16 '18 at 17:33
  • They did not provide any example for axis in this context. – Ravi Aug 16 '18 at 18:36
  • 4
    axis=0 means rearrange the rows and axis=1 means rearrange the columns. by=1 tells the columns should be reorderedby row 1. So if you check the output you will see that row 1 is now in increasing order. – ayhan Aug 16 '18 at 19:36

1 Answers1

5

The parameter axis=1 refer to columns, while 0 refers to rows. In this case you are sorting by columns, specifically index 1, which is col2 (indexing in python starts at 0).

Some good examples here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

Nick Tallant
  • 315
  • 3
  • 6
  • How are you so sure that col2 is index for df? – mad_ Aug 16 '18 at 17:45
  • @mad_ Didn't mean for it to sound like col2 is the index, the DataFrame has an actual index. Just meant that for the columns here as `['col1', 'col2', 'col3']`, `'col2'` is at position 1. – Nick Tallant Aug 16 '18 at 18:04