Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing sort in value_counts

If I do

mt = mobile.PattLen.value_counts()   # sort True by default 

I get

4    2831 3    2555  5    1561 [...] 

If I do

mt = mobile.PattLen.value_counts(sort=False)  

I get

8    225 9    120 2   1234  [...] 

What I am trying to do is get the output in 2, 3, 4 ascending order (the left numeric column). Can I change value_counts somehow or do I need to use a different function.

like image 278
Mark Ginsburg Avatar asked May 08 '17 19:05

Mark Ginsburg


People also ask

Does value_counts sort?

The value_counts() function is used to get a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occurring element.

How do you sort PD series?

Sort the Series in Ascending Order By default, the pandas series sort_values() function sorts the series in ascending order. You can also use ascending=True param to explicitly specify to sort in ascending order. Also, if you have any NaN values in the Series, it sort by placing all NaN values at the end.

What is normalize in value_counts?

value_counts() with relative frequencies of the unique values. Sometimes, getting a percentage is a better criterion then the count. By setting normalize=True , the object returned will contain the relative frequencies of the unique values. The normalize parameter is set to False by default.

How do I sort a column by DF?

Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.


2 Answers

I think you need sort_index, because the left column is called index. The full command would be mt = mobile.PattLen.value_counts().sort_index(). For example:

mobile = pd.DataFrame({'PattLen':[1,1,2,6,6,7,7,7,7,8]}) print (mobile)    PattLen 0        1 1        1 2        2 3        6 4        6 5        7 6        7 7        7 8        7 9        8  print (mobile.PattLen.value_counts()) 7    4 6    2 1    2 8    1 2    1 Name: PattLen, dtype: int64   mt = mobile.PattLen.value_counts().sort_index() print (mt) 1    2 2    1 6    2 7    4 8    1 Name: PattLen, dtype: int64 
like image 91
jezrael Avatar answered Nov 16 '22 01:11

jezrael


As hinted by normanius’ comment under jezrael’s answer :

>>> df = pd.DataFrame({"a":[1,1,2,6,6,7,7,7,7,8]}) >>> df.a.value_counts()[df.a.unique()] 1    2 2    1 6    2 7    4 8    1 Name: a, dtype: int64 

one can sort by any order by providing a custom index explicitely :

>>> df.a.value_counts()[[8,7,6,2,1]] 8    1 7    4 6    2 2    1 1    2 Name: a, dtype: int64 >>> df.a.value_counts()[[1,8,6,2,7]] 1    2 8    1 6    2 2    1 7    4 Name: a, dtype: int64 

This is of particular interest for plotting categorical data :

>>> df.a.value_counts()[['hourly','daily','weekly','monthly']].plot(type="bar") 

Anecdotically, it can be used to remove some entries or to make others appear several times :

>>> df.a.value_counts()[[1,1,1,8]] 1    2 1    2 1    2 8    1 Name: a, dtype: int64 
like image 21
Skippy le Grand Gourou Avatar answered Nov 16 '22 01:11

Skippy le Grand Gourou