/ Python And R Data science skills: 57 pivot tables
Showing posts with label 57 pivot tables. Show all posts
Showing posts with label 57 pivot tables. Show all posts

Saturday, 10 February 2018

57 pivot tables In Python

https://vlrtraining.com/courses/python-data-science-beginner-tutorial 57 pivot tables
In [5]:
import pandas as pd
data = {'A':['foo','foo','foo','bar','bar','bar'],
     'B':['one','one','two','two','one','one'],
       'C':['x','y','x','y','x','y'],
       'D':[1,3,2,5,4,1]}

df = pd.DataFrame(data)
In [6]:
df
Out[6]:
A B C D
0 foo one x 1
1 foo one y 3
2 foo two x 2
3 bar two y 5
4 bar one x 4
5 bar one y 1
In [3]:
df.pivot_table(values='D',index=['A', 'B'],columns=['C'])
Out[3]:
C x y
A B
bar one 4.0 1.0
two NaN 5.0
foo one 1.0 3.0
two 2.0 NaN
In [7]:
pwd
Out[7]:
'C:\\Users\\venkat\\Desktop\\my python ds'