Home > Software engineering >  sort and count values in a column DataFrame (Python Pandas)
sort and count values in a column DataFrame (Python Pandas)

Time:06-30

I have the next DataFrame

df

enter image description here

I count the values this way

enter image description here

I want to have the category values in the next order:

1.0 1

4.0 1

7.0 2

10.0 1

and so on ...

In the ascending way with their respect amount of values

CodePudding user response:

You can sort by index using sort_index()

df['col_1'].value_counts().sort_index()

CodePudding user response:

You can sort on the index after calling value_counts. here's an example

df = pd.DataFrame({'x':[1,2,2,2,1,4,5,5,4,3,5,6,3]})
df['x'].value_counts().sort_index()

Output:

1    2
2    3
3    2
4    2
5    3
6    1
Name: x, dtype: int64

CodePudding user response:

Created Data frame having values as below :

enter image description here

and then sort it via below code ,below is output shown

df1.groupby(by=['Cat']).count().sort_values(by='col1')

enter image description here

  • Related