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 7598777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:29:36+00:00 2026-05-30T22:29:36+00:00

I am trying to do a pivot table of frequency counts using Pandas. I

  • 0

I am trying to do a pivot table of frequency counts using Pandas.

I have the following code:

 from pandas import pivot_table, DataFrame, crosstab
 import numpy as np
 df=DataFrame(
    {'Y':[99999991, 99999992, 99999993, 99999994, 99999995,
          99999996, 99999997, 99999998, 99999999],
     'X':[1, 2, 3, 4, 5, 6, 7, 8, 9],
     'X2':[1, 2, 3, 4, 5, 6, 7, 8, 9]})
 print pivot_table(df,rows=['Y'], cols=['X'],aggfunc=np.sum)

This is my output:

 X          1   2   3   4   5   6   7   8   9
 Y                                         
  99999991   1 NaN NaN NaN NaN NaN NaN NaN NaN
  99999992 NaN   2 NaN NaN NaN NaN NaN NaN NaN
  99999993 NaN NaN   3 NaN NaN NaN NaN NaN NaN
  99999994 NaN NaN NaN   4 NaN NaN NaN NaN NaN
  99999995 NaN NaN NaN NaN   5 NaN NaN NaN NaN
  99999996 NaN NaN NaN NaN NaN   6 NaN NaN NaN
  99999997 NaN NaN NaN NaN NaN NaN   7 NaN NaN
  99999998 NaN NaN NaN NaN NaN NaN NaN   8 NaN
  99999999 NaN NaN NaN NaN NaN NaN NaN NaN   9

This is my desired output:

 X          1   2   3   4   5   6   7   8   9
 X2                                         
   1   99999991 NaN NaN NaN NaN NaN NaN NaN NaN
   2 NaN   99999992 NaN NaN NaN NaN NaN NaN NaN
   3 NaN NaN   99999993 NaN NaN NaN NaN NaN NaN
   4 NaN NaN NaN   99999994 NaN NaN NaN NaN NaN
   5 NaN NaN NaN NaN   99999995 NaN NaN NaN NaN
   6 NaN NaN NaN NaN NaN   99999996 NaN NaN NaN
   7 NaN NaN NaN NaN NaN NaN   99999997 NaN NaN
   8 NaN NaN NaN NaN NaN NaN NaN   99999998 NaN
   9 NaN NaN NaN NaN NaN NaN NaN NaN   99999999

This is what I keep getting:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 9 entries, 1 to 9
Data columns:
('Y', 1L)    1  non-null values
('Y', 2L)    1  non-null values
('Y', 3L)    1  non-null values
('Y', 4L)    1  non-null values
('Y', 5L)    1  non-null values
('Y', 6L)    1  non-null values
('Y', 7L)    1  non-null values
('Y', 8L)    1  non-null values
('Y', 9L)    1  non-null values
dtypes: float64(9)

Does anyone know why? Is the output too big. I can’t seem to find anything on it.

  • 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-05-30T22:29:37+00:00Added an answer on May 30, 2026 at 10:29 pm

    Just replace rows=['Y'] with rows=['X2']

    >>> print pivot_table(df,rows=['X2'], cols=['X'],aggfunc=np.sum)
          Y                                        
    X     1    2    3    4    5    6    7    8    9
    X2                                             
    1   101  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
    2   NaN  102  NaN  NaN  NaN  NaN  NaN  NaN  NaN
    3   NaN  NaN  103  NaN  NaN  NaN  NaN  NaN  NaN
    4   NaN  NaN  NaN  104  NaN  NaN  NaN  NaN  NaN
    5   NaN  NaN  NaN  NaN  105  NaN  NaN  NaN  NaN
    6   NaN  NaN  NaN  NaN  NaN  106  NaN  NaN  NaN
    7   NaN  NaN  NaN  NaN  NaN  NaN  107  NaN  NaN
    8   NaN  NaN  NaN  NaN  NaN  NaN  NaN  108  NaN
    9   NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  109
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to move away from using DECODE to pivot rows in
Does anyone have an example of a pivot using a table with an XML
I am using excel 2010. I am trying to create pivot table between two
I'm trying to use GetPivotData to query Foo Bar values from a pivot table.
I have a pivot table with some data that im trying to reference with
I'm trying to remove relationships from a pivot table with ORM's remove method. This
I am trying to create a pivot table type view in postgresql and am
I am trying to make a pivot table with rollup and a case statement
I have a query where I'm trying pivot row values into column names and
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,

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.