Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:45:42+00:00 2026-06-05T07:45:42+00:00

I have a dataframe from a multiple choice questions and it is formatted like

  • 0

I have a dataframe from a multiple choice questions and it is formatted like so:

      Sex Qu1  Qu2  Qu3
Name
Bob    M   1    2    1
John   M   3    3    5
Alex   M   4    1    2
Jen    F   3    2    4
Mary   F   4    3    4

The data is a rating from 1 to 5 for the 3 multiple choice questions. I want rearrange the data so that the index is range(1,6) where 1=’bad’, 2=’poor’, 3=’ok’, 4=’good’, 5=’excellent’, the columns are the same and the data is the count of the number occurrences of the values (excluding the Sex column). This is basically a histogram of fixed bin sizes and the x-axis labeled with strings. I like the output of df.plot() much better than df.hist() for this but I can’t figure out how to rearrange the table to give me a histogram of data. Also, how do you change x-labels to be strings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:45:43+00:00Added an answer on June 5, 2026 at 7:45 am

    Series.value_counts gives you the histogram you’re looking for:

    In [9]: df['Qu1'].value_counts()
    Out[9]: 
    4    2
    3    2
    1    1
    

    So, apply this function to each of those 3 columns:

    In [13]: table = df[['Qu1', 'Qu2', 'Qu3']].apply(lambda x: x.value_counts())
    
    In [14]: table
    Out[14]: 
       Qu1  Qu2  Qu3
    1    1    1    1
    2  NaN    2    1
    3    2    2  NaN
    4    2  NaN    2
    5  NaN  NaN    1
    
    In [15]: table = table.fillna(0)
    
    In [16]: table
    Out[16]: 
       Qu1  Qu2  Qu3
    1    1    1    1
    2    0    2    1
    3    2    2    0
    4    2    0    2
    5    0    0    1
    

    Using table.reindex or table.ix[some_array] you can rearrange the data.

    To transform to strings, use table.rename:

    In [17]: table.rename(index=str)
    Out[17]: 
       Qu1  Qu2  Qu3
    1    1    1    1
    2    0    2    1
    3    2    2    0
    4    2    0    2
    5    0    0    1
    
    In [18]: table.rename(index=str).index[0]
    Out[18]: '1'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe called ants detailing multiple entries per site, looks like this:
I have a small dataframe from which I am plotting 3 columns in order
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe with a column of integers that I would like to
I have a dataframe and would like to add a new column where the
I have a dataframe in R that I loaded from a CSV file. One
I have a dataframe with the lengths and widths of various arthropods from the
I have a dataframe (14.5K rows by 15 columns) containing billing data from 2001
I want to create a single shape file from multiple mxd's that have multiple
I have a dataframe foo looking like this Date Return 1998-01-01 0.02 1998-01-02 0.04

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.