ken
ken

Reputation: 21

Sorting values in a dataframe

I've been trying to sort the values on my a dataframe that I've been given to work on.

The following is my dataframe.

1981    1.78
1982    1.74
1983    1.61
1984    1.62
1985    1.61
1986    1.43
1987    1.62
1988    1.96
1989    1.75
1990    1.83
1991    1.73
1992    1.72
1993    1.74
1994    1.71
1995    1.67
1996    1.66
1997    1.61
1998    1.48
1999    1.47
2000     1.6
2001    1.41
2002    1.37
2003    1.27
2004    1.26
2005    1.26
2006    1.28
2007    1.29
2008    1.28
2009    1.22
2010    1.15
2011     1.2
2012    1.29
2013    1.19
2014    1.25
2015    1.24
2016     1.2
2017    1.16
2018    1.14

I've been trying to sort my dataframe in descending order such that the highest values on the right would appear first. However whenever I try to sort it, it would only sort based on the year which are the values on the left.

dataframe.sort_values('1')

I've tried using sort_values and indicating '1' as the column that I want sorted. This however returns ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'>

Upvotes: 1

Views: 47

Answers (2)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

From the error that OP mentioned, The data structure is a Series and hence the sort function should just be called directly

s = s.sort_values(ascending=False)

The error was raised because, in pandas.Series.sort_values the first argument is axis.

Upvotes: 1

Georgina Skibinski
Georgina Skibinski

Reputation: 13397

The argument of sort_values() should be column name:

df=df.sort_values("col2")

Upvotes: 0

Related Questions