[PYTHON] A simple sample of pivot_table.

test data

{"date":"1/2" , "Full name":"Yamada" ,"dessert" : "Apple"  ,"Side dishes"  :"Gyoza" },
{"date":"1/3" , "Full name":"Yamada" ,"dessert" : "Apple"  ,"Side dishes"  :"Gyoza"   },
{"date":"1/4" , "Full name":"Yamada" ,"dessert" : "Banana"  ,"Side dishes"  :"Boiled fish"   },
{"date":"1/5" , "Full name":"Yamada" ,"dessert" : "Apple"   ,"Side dishes"  :"Gyoza"  },

{"date":"1/2" , "Full name":"Sasaki" ,"dessert" : "Orange"   ,"Side dishes"  :"Gyoza"  },
{"date":"1/3" , "Full name":"Sasaki" ,"dessert" : "Apple"   ,"Side dishes"  :"Gyoza"  },
{"date":"1/4" , "Full name":"Sasaki" ,"dessert" : "Apple"   ,"Side dishes"  :"Gyoza"  },
{"date":"1/5" , "Full name":"Sasaki" ,"dessert" : "Apple"   ,"Side dishes"  :"stew"  },


import pandas as pd

Date Name Dessert Side dish
0  1/2 Yamada apple dumplings
1  1/3 Yamada apple dumplings
2  1/4 Yamada Banana Boiled fish
3  1/5 Yamada apple dumplings
4  1/2 Sasaki orange dumplings
5  1/3 Sasaki apple dumplings
6  1/4 Sasaki apple dumplings
7  1/5 Sasaki apple stew

Count the "desserts" for each "name".

df.pivot_table(values= 'Side dishes', index='Full name', columns='dessert' , aggfunc="count" , fill_value=0)

values = If you do not enter'side dishes', it will be aggregated for each remaining column Number of cases with aggfunc = "count" If fill_value = 0 is not entered, 0 will be NaN.


Dessert Banana Apple Mandarin
Full name
Sasaki 0 3 1
Yamada 1 3 0

reference note.nkmk.me pandas.pivot_table

Recommended Posts

A simple sample of pivot_table.
Implementation of a simple particle filter
A small sample note of list_head
Pandas: A very simple example of DataFrame.rolling ()
A simple example of how to use ArgumentParser
Sample to draw a simple clock using ebiten
Python --Simple multi-thread sample
This is a sample of function application in dataframe.
Accelerate a large number of simple queries with MySQL
A very simple example of an ortoolpy optimization problem
Super simple: A collection of shells that output dates
A note of trying a simple MCMC tutorial on PyMC3
A simple Python implementation of the k-nearest neighbor method (k-NN)
A memorandum of kernel compilation
I just changed the sample source of Python a little.
Create a (simple) REST server
A small memorandum of openpyxl
Simple FPS measurement of python
Simple simulation of virus infection
Sample usage of Python pickle
Create a simple textlint server
A brief summary of Linux
A memorandum of using eigen3
A simple data analysis of Bitcoin provided by CoinMetrics in Python
Evaluate the performance of a simple regression model using LeaveOneOut cross-validation
Read the config file in Go language! Introducing a simple sample
[Python] A simple function to find the center coordinates of a circle
A good description of Python decorators
Learn Zundokokiyoshi using a simple RNN
Implementation of a two-layer neural network 2
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
A collection of one-liner web servers
Creating a simple table using prettytable
Explanation and implementation of simple perceptron
Creating a simple app with flask
A memorandum of files under conf.d
A rough summary of OS history
A brief summary of qubits (beginners)
A Tour of Go Learning Summary
Implementing a simple algorithm in Python 2
Write a super simple TCP server
Run a simple algorithm in Python
Generate a list of consecutive characters
A memorandum of closure survey contents
The story of writing a program
I made a kind of simple image processing tool in Go language.