Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Preserving order with pandas.crosstab

I have the following csv data:

question,answer
m2020_s,3
m2020_s,3
m2020_s,3
m2020_s,3
m2020_s,3
m2020_s,3
a2020_k,1
a2020_k,2
a2020_k,1
a2020_k,4
a2020_k,1
a2020_k,1
d2015_a,5
d2015_a,4
d2015_a,4
d2015_a,4
d2015_a,4
d2015_a,4

I’m using pd.crosstab to count the number of times each answer was given but the function is messing with the order of my data. Here is my code:

import pandas as pd

df = pd.read_csv('example.csv')

output_array = pd.crosstab(df['question'], df['answer']).to_numpy()

print(output_array)

Expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[0 0 6 0 0]
 [4 1 0 1 0]
 [0 0 0 5 1]]

Actual result:

[[4 1 0 1 0]
 [0 0 0 5 1]
 [0 0 6 0 0]]

Why is this happening? And how can I preserve the data’s order?

>Solution :

Could you try this,

pd.crosstab(df['question'], df['answer']).reindex(df['question'].unique()).to_numpy()

O/P:

array([[0, 0, 6, 0, 0],
       [4, 1, 0, 1, 0],
       [0, 0, 0, 5, 1]], dtype=int64)

Explanation:
Reorder index based on unique elements in your dataset based on first occurance.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading