87938

How to pivot_table with with duplicated index

Question:

I have a df_ like this,

name level status yes high open no high closed no med closed yes low open no med rejected no high open

I am trying to create a pivot table with index='level',columns='status', values=sum of occurances with respect to the column and index

my code:

df_['temp']=df_['level'].astype(bool).astype(int) df_.pivot(index='level',columns='status',values='temp')

but gives me, ValueError: Index contains duplicate entries, cannot reshape

My expected output is,

open closed rejected high 2 1 0 med 0 1 1 low 1 0 0

Please check and tell me if there is any other easy way.

Answer1:

A simpler approach would be to count the occurrences of name:

df_.pivot_table(values='name', index='level', columns='status', aggfunc='count', fill_value=0)

Recommend

  • How to extract specific sequences from a dataframe?
  • preg_match_all and newlines inside quotes
  • prepend href with same class
  • Pass parameters to temp variables in MS Query on SQL Server from Excel
  • How to diagnose metro app deployment errors?
  • Extend a regular expression to negative number
  • SQL - Select lowest values with group by and order by?
  • Hash Code in SQL Server?
  • Primefaces :radioButton inside a ui:repeat
  • Can't remove headers after they are sent
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Approximate Order-Preserving Huffman Code
  • Ember.js model to be organised as a tree structure
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • How to use JavaScript to determine whether a file exists in a directory?
  • Grails calculated field in SQL
  • Groovy: Unexpected token “:”
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Replace value with Factor in r data.table
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Is possible to count alias result on mysql
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Convert array of 8 bytes to signed long in C++
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Unanticipated behavior
  • Comma separated Values
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • Understanding cpu registers
  • How can I remove ASP.NET Designer.cs files?
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal