Pandas Series value_counts working differently for different counts
up vote
6
down vote
favorite
For example:
df1 = pd.DataFrame(np.repeat(np.arange(1,7),3), columns=['A'])
df1.A.value_counts(sort=False)
1 3
2 3
3 3
4 3
5 3
6 3
Name: A, dtype: int64
df2 = pd.DataFrame(np.repeat(np.arange(1,7),100), columns=['A'])
df2.A.value_counts(sort=False)
1 100
2 100
3 100
4 100
5 100
6 100
Name: A, dtype: int64
In the above examples the value_counts works perfectly and give the required result. whereas when coming to larger dataframes it is giving a different output. Here the A values are already sorted and counts are also same, but the order of index that is A changed after value_counts. Why is it doing correctly for small counts but not for large counts:
df3 = pd.DataFrame(np.repeat(np.arange(1,7),1000), columns=['A'])
df3.A.value_counts(sort=False)
4 1000
1 1000
5 1000
2 1000
6 1000
3 1000
Name: A, dtype: int64
Here I can do df3.A.value_counts(sort=False).sort_index() or df3.A.value_counts(sort=False).reindex(df.A.unique()). I want to know the reason why it is behaving differently for different counts?
Using:
Numpy version :1.15.2
Pandas version :0.23.4
python pandas numpy
add a comment |
up vote
6
down vote
favorite
For example:
df1 = pd.DataFrame(np.repeat(np.arange(1,7),3), columns=['A'])
df1.A.value_counts(sort=False)
1 3
2 3
3 3
4 3
5 3
6 3
Name: A, dtype: int64
df2 = pd.DataFrame(np.repeat(np.arange(1,7),100), columns=['A'])
df2.A.value_counts(sort=False)
1 100
2 100
3 100
4 100
5 100
6 100
Name: A, dtype: int64
In the above examples the value_counts works perfectly and give the required result. whereas when coming to larger dataframes it is giving a different output. Here the A values are already sorted and counts are also same, but the order of index that is A changed after value_counts. Why is it doing correctly for small counts but not for large counts:
df3 = pd.DataFrame(np.repeat(np.arange(1,7),1000), columns=['A'])
df3.A.value_counts(sort=False)
4 1000
1 1000
5 1000
2 1000
6 1000
3 1000
Name: A, dtype: int64
Here I can do df3.A.value_counts(sort=False).sort_index() or df3.A.value_counts(sort=False).reindex(df.A.unique()). I want to know the reason why it is behaving differently for different counts?
Using:
Numpy version :1.15.2
Pandas version :0.23.4
python pandas numpy
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
For example:
df1 = pd.DataFrame(np.repeat(np.arange(1,7),3), columns=['A'])
df1.A.value_counts(sort=False)
1 3
2 3
3 3
4 3
5 3
6 3
Name: A, dtype: int64
df2 = pd.DataFrame(np.repeat(np.arange(1,7),100), columns=['A'])
df2.A.value_counts(sort=False)
1 100
2 100
3 100
4 100
5 100
6 100
Name: A, dtype: int64
In the above examples the value_counts works perfectly and give the required result. whereas when coming to larger dataframes it is giving a different output. Here the A values are already sorted and counts are also same, but the order of index that is A changed after value_counts. Why is it doing correctly for small counts but not for large counts:
df3 = pd.DataFrame(np.repeat(np.arange(1,7),1000), columns=['A'])
df3.A.value_counts(sort=False)
4 1000
1 1000
5 1000
2 1000
6 1000
3 1000
Name: A, dtype: int64
Here I can do df3.A.value_counts(sort=False).sort_index() or df3.A.value_counts(sort=False).reindex(df.A.unique()). I want to know the reason why it is behaving differently for different counts?
Using:
Numpy version :1.15.2
Pandas version :0.23.4
python pandas numpy
For example:
df1 = pd.DataFrame(np.repeat(np.arange(1,7),3), columns=['A'])
df1.A.value_counts(sort=False)
1 3
2 3
3 3
4 3
5 3
6 3
Name: A, dtype: int64
df2 = pd.DataFrame(np.repeat(np.arange(1,7),100), columns=['A'])
df2.A.value_counts(sort=False)
1 100
2 100
3 100
4 100
5 100
6 100
Name: A, dtype: int64
In the above examples the value_counts works perfectly and give the required result. whereas when coming to larger dataframes it is giving a different output. Here the A values are already sorted and counts are also same, but the order of index that is A changed after value_counts. Why is it doing correctly for small counts but not for large counts:
df3 = pd.DataFrame(np.repeat(np.arange(1,7),1000), columns=['A'])
df3.A.value_counts(sort=False)
4 1000
1 1000
5 1000
2 1000
6 1000
3 1000
Name: A, dtype: int64
Here I can do df3.A.value_counts(sort=False).sort_index() or df3.A.value_counts(sort=False).reindex(df.A.unique()). I want to know the reason why it is behaving differently for different counts?
Using:
Numpy version :1.15.2
Pandas version :0.23.4
python pandas numpy
python pandas numpy
edited Nov 14 at 6:29
asked Nov 14 at 6:22
Sandeep Kadapa
5,519427
5,519427
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
This is actually a known problem.
If you browse through the source code -
C:ProgramDataAnaconda3Libsite-packagespandascorealgorithims.pyline581is the original implementation- It calls
_value_counts_arraylikeforint64values whenbins=None
- This function makes a call -
keys, counts = htable.value_count_int64(values, dropna)
If you then look at the htable implementation you will conclude that the keys are in an arbitrary order, subject to how the hashtable works.
Its not a guarantee of ANY kind of ordering. Typically this routine sorts by biggest values, and that is almost always what you want.
I guess they can change this to have sort=False mean original ordering. I don't know if this would actually break anything (and done internally this isn't very costly as the uniques are already known).
The order is changed from pandas/hashtable.pyx.build_count_table_object(). Resizing of the pymap moves the entries by hashing values.
Here is the full discussion
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
1
It looks to be some update for orignal order retaining github page look to be using same update asreindex(unique(values)):-) that's only way, So one have to reindex to preseve the orignal ordering
– pygo
Nov 14 at 8:16
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
This is actually a known problem.
If you browse through the source code -
C:ProgramDataAnaconda3Libsite-packagespandascorealgorithims.pyline581is the original implementation- It calls
_value_counts_arraylikeforint64values whenbins=None
- This function makes a call -
keys, counts = htable.value_count_int64(values, dropna)
If you then look at the htable implementation you will conclude that the keys are in an arbitrary order, subject to how the hashtable works.
Its not a guarantee of ANY kind of ordering. Typically this routine sorts by biggest values, and that is almost always what you want.
I guess they can change this to have sort=False mean original ordering. I don't know if this would actually break anything (and done internally this isn't very costly as the uniques are already known).
The order is changed from pandas/hashtable.pyx.build_count_table_object(). Resizing of the pymap moves the entries by hashing values.
Here is the full discussion
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
1
It looks to be some update for orignal order retaining github page look to be using same update asreindex(unique(values)):-) that's only way, So one have to reindex to preseve the orignal ordering
– pygo
Nov 14 at 8:16
add a comment |
up vote
2
down vote
accepted
This is actually a known problem.
If you browse through the source code -
C:ProgramDataAnaconda3Libsite-packagespandascorealgorithims.pyline581is the original implementation- It calls
_value_counts_arraylikeforint64values whenbins=None
- This function makes a call -
keys, counts = htable.value_count_int64(values, dropna)
If you then look at the htable implementation you will conclude that the keys are in an arbitrary order, subject to how the hashtable works.
Its not a guarantee of ANY kind of ordering. Typically this routine sorts by biggest values, and that is almost always what you want.
I guess they can change this to have sort=False mean original ordering. I don't know if this would actually break anything (and done internally this isn't very costly as the uniques are already known).
The order is changed from pandas/hashtable.pyx.build_count_table_object(). Resizing of the pymap moves the entries by hashing values.
Here is the full discussion
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
1
It looks to be some update for orignal order retaining github page look to be using same update asreindex(unique(values)):-) that's only way, So one have to reindex to preseve the orignal ordering
– pygo
Nov 14 at 8:16
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
This is actually a known problem.
If you browse through the source code -
C:ProgramDataAnaconda3Libsite-packagespandascorealgorithims.pyline581is the original implementation- It calls
_value_counts_arraylikeforint64values whenbins=None
- This function makes a call -
keys, counts = htable.value_count_int64(values, dropna)
If you then look at the htable implementation you will conclude that the keys are in an arbitrary order, subject to how the hashtable works.
Its not a guarantee of ANY kind of ordering. Typically this routine sorts by biggest values, and that is almost always what you want.
I guess they can change this to have sort=False mean original ordering. I don't know if this would actually break anything (and done internally this isn't very costly as the uniques are already known).
The order is changed from pandas/hashtable.pyx.build_count_table_object(). Resizing of the pymap moves the entries by hashing values.
Here is the full discussion
This is actually a known problem.
If you browse through the source code -
C:ProgramDataAnaconda3Libsite-packagespandascorealgorithims.pyline581is the original implementation- It calls
_value_counts_arraylikeforint64values whenbins=None
- This function makes a call -
keys, counts = htable.value_count_int64(values, dropna)
If you then look at the htable implementation you will conclude that the keys are in an arbitrary order, subject to how the hashtable works.
Its not a guarantee of ANY kind of ordering. Typically this routine sorts by biggest values, and that is almost always what you want.
I guess they can change this to have sort=False mean original ordering. I don't know if this would actually break anything (and done internally this isn't very costly as the uniques are already known).
The order is changed from pandas/hashtable.pyx.build_count_table_object(). Resizing of the pymap moves the entries by hashing values.
Here is the full discussion
answered Nov 14 at 6:44
Vivek Kalyanarangan
4,3881726
4,3881726
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
1
It looks to be some update for orignal order retaining github page look to be using same update asreindex(unique(values)):-) that's only way, So one have to reindex to preseve the orignal ordering
– pygo
Nov 14 at 8:16
add a comment |
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
1
It looks to be some update for orignal order retaining github page look to be using same update asreindex(unique(values)):-) that's only way, So one have to reindex to preseve the orignal ordering
– pygo
Nov 14 at 8:16
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
When keys are in an arbitrary order, it must not guarantee for any number of counts right? but if the counts are small it is preserving the order and if the counts are high it isn't. Also, it maintains order till 341 counts but fails after it.
– Sandeep Kadapa
Nov 14 at 7:05
1
1
It looks to be some update for orignal order retaining github page look to be using same update as
reindex(unique(values)) :-) that's only way, So one have to reindex to preseve the orignal ordering– pygo
Nov 14 at 8:16
It looks to be some update for orignal order retaining github page look to be using same update as
reindex(unique(values)) :-) that's only way, So one have to reindex to preseve the orignal ordering– pygo
Nov 14 at 8:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294222%2fpandas-series-value-counts-working-differently-for-different-counts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown