├── README.md ├── Volume III ├── 4-Matplotlib │ ├── Code_Scripts │ │ ├── 4.1-PyplotAPI.py │ │ ├── 4.2-Pylab module.py │ │ ├── 4.3-Figure and axe class.py │ │ ├── 4.4-Subplots.py │ │ ├── 4.5-Type of charts.py │ │ ├── 4.6-One-Dimensional Plots.ipynb │ │ ├── 4.6-One-Dimensional Plots.py │ │ ├── 4.7-Two-dimensional plots.py │ │ ├── 4.8-Three-dimensional Plotting.py │ │ ├── 4.9-Working with Images.py │ │ ├── diabetes.csv │ │ └── txt. │ └── Images │ │ ├── 4.0-Jupyter notebook │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ └── 5.jpg │ │ ├── 4.1-PyplotAPI │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ └── 3.jpg │ │ ├── 4.2-Pylab module │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ └── 3.jpg │ │ ├── 4.3-Figure and axe class │ │ └── 1.jpg │ │ ├── 4.4-Subplots │ │ ├── 1.jpg │ │ └── 2.jpg │ │ ├── 4.5-Types Of Charts │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ └── 5.jpg │ │ ├── 4.6-Onedimensional plots │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ ├── 5.jpg │ │ ├── 6.jpg │ │ └── 7.jpg │ │ ├── 4.7-Twodimensional plots │ │ ├── 1.jpg │ │ └── 2.jpg │ │ ├── 4.8-Three dimensional plots │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ ├── 5.jpg │ │ ├── 6.jpg │ │ ├── 7.jpg │ │ └── 8.jpg │ │ └── 4.9-Working with images │ │ ├── 1.jpg │ │ └── 2.jpg ├── 5-Seaborn │ ├── Code_Scripts │ │ ├── 5.0-first seaborn plot.py │ │ ├── 5.1-Seaborn Styling,Figure Style and Scale.py │ │ └── 5.2-Plot varieties │ │ │ ├── Pie Chart & Bar Chart.py │ │ │ ├── Plot Varieties.py │ │ │ ├── googleplaystore.csv │ │ │ └── iris.csv │ └── Images │ │ ├── 5.0-Introduction │ │ ├── 1.jpg │ │ ├── 10.jpg │ │ ├── 11.jpg │ │ ├── 12.jpg │ │ ├── 13.jpg │ │ ├── 14.jpg │ │ ├── 15.jpg │ │ ├── 16.jpg │ │ ├── 17.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ ├── 5.jpg │ │ ├── 6.jpg │ │ ├── 7.jpg │ │ ├── 8.jpg │ │ └── 9.jpg │ │ ├── 5.1-Styling and scaling │ │ ├── 1.jpg │ │ ├── 10.jpg │ │ ├── 11.jpg │ │ ├── 12.jpg │ │ ├── 13.jpg │ │ ├── 14.jpg │ │ ├── 15.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ ├── 5.jpg │ │ ├── 6.jpg │ │ ├── 7.jpg │ │ ├── 8.jpg │ │ └── 9.jpg │ │ └── 5.2-Plot Varities │ │ ├── 1.jpg │ │ ├── 10.jpg │ │ ├── 11.jpg │ │ ├── 12.jpg │ │ ├── 13.jpg │ │ ├── 14.jpg │ │ ├── 15.jpg │ │ ├── 16.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ ├── 5.jpg │ │ ├── 6.jpg │ │ ├── 7.jpg │ │ ├── 8.jpg │ │ └── 9.jpg └── 6-ggplot │ ├── Code_Scripts │ ├── 6.0-Introduction │ │ └── ggplot.ipynb │ └── 6.1-Layers │ │ ├── Layers.ipynb │ │ └── london2018.csv │ └── Images │ ├── 6.0-Introduction │ ├── 1.jpg │ ├── 10.jpg │ ├── 11.jpg │ ├── 12.jpg │ ├── 13.jpg │ ├── 14.jpg │ ├── 15.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ └── 9.jpg │ └── 6.1-Layers │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ ├── 9.jpg │ ├── Table1.jpg │ └── Table2.jpg ├── Volume_I └── 2-Numpy │ ├── Code_Scripts │ ├── 2.1-Ndarray Object .py │ ├── 2.10-Sort.py │ ├── 2.11-Byte Swapping.py │ ├── 2.12-Matrix Library.py │ ├── 2.13-NumPy Matplotlib.py │ ├── 2.14-Histogram Using Matplotlib.py │ ├── 2.2-Array Attributes.py │ ├── 2.3-Array From Existing Data.py │ ├── 2.4-Indexing & Slicing.py │ ├── 2.5-Broadcasting.py │ ├── 2.7-Mathematical Functions.py │ ├── 2.8-Arithmetic Operations.py │ └── 2.9-Statistical Functions.py │ └── Images │ ├── 2.1-Ndarray object │ └── 1.jpg │ ├── 2.13-Numpy matplotlib │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ └── 5.jpg │ ├── 2.14-Numpy histogram │ └── 1.jpg │ └── 2.5-Numpy Broadcasting │ └── 1.jpg └── Volume_II └── 3-Pandas ├── Code_Scripts ├── 3.1-Data Structures.py ├── 3.10-Indexing and Selecting Data.py ├── 3.11-Statistical Functions.py ├── 3.12-Aggregation.py ├── 3.13-Missing Data.py ├── 3.14-GroupBy.py ├── 3.15-Merging or Joining.py ├── 3.16-Concatenation.py ├── 3.17-Data Functionality.py ├── 3.18-Categorical Data.py ├── 3.19-Visualization.py ├── 3.2-DataFrame.py ├── 3.20-Comparison with SQL │ ├── Comparison with SQL.py │ └── tips.csv ├── 3.3-Basic Functionality.py ├── 3.4-Descriptive Statistics.py ├── 3.5-Function Application.py ├── 3.6-Reindexing and Iteration.py ├── 3.7-Pandas Sorting.py ├── 3.8-Working with Text Data.py └── 3.9-Options and Customization.py └── Images ├── 3.19-Pandas visualization ├── 1.jpg ├── 10.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg ├── 6.jpg ├── 7.jpg ├── 8.jpg └── 9.jpg └── 3.2-Pandas Dataframe └── 1.jpg /README.md: -------------------------------------------------------------------------------- 1 | # Data Exploration and Analysis Book in Urdu 2 | ## Authors: Sana Rasheed & Zeeshan-ul-hassan Usmani 3 | 4 | ## Dear Readers, 5 | 6 | Thank you for stepping in the domain of Data Science. I hope, it will give you a head start to Data Science domain by learning techniques of exploring data and data analysis. 7 | 8 | Code scripts in Python programming language are available for book readers for easy learning and coding. 9 | 10 | This book has been divided into 3 volumes: 11 | 1. Data Exploration and Analysis - Numpy 12 | 2. Data Exploration and Analysis - Pandas 13 | 3. Data Exploration and Analysis - Matplotlib, Seaborn, and ggplot 14 | 15 | This repository contains 3 folders based on defined volumes and subsequent folder contains Python code script and images used in book. 16 | 17 | 18 | ### Pre-requisite: Python Programming Language 19 | 20 | ## Happy Learning and Keep Practising! 21 | 22 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.1-PyplotAPI.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | from matplotlib import pyplot as plt 8 | import numpy as np 9 | import math #needed for definition of pi 10 | x = np.arange(0, math.pi*2, 0.05) 11 | y = np.sin(x) 12 | plt.plot(x,y) 13 | plt.xlabel("angle") 14 | plt.ylabel("sine") 15 | plt.title('sine wave') 16 | plt.show() 17 | 18 | 19 | # In[2]: 20 | 21 | 22 | from matplotlib import pyplot as plt 23 | import numpy as np 24 | get_ipython().run_line_magic('matplotlib', 'inline') 25 | import math 26 | x = np.arange(0, math.pi*2, 0.05) 27 | y = np.sin(x) 28 | plt.xlabel("angle") 29 | plt.ylabel("sine") 30 | plt.title('sine wave') 31 | plt.plot(x,y) 32 | 33 | 34 | # In[ ]: 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.2-Pylab module.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## PyLab module 5 | 6 | # ### Basic Plotting 7 | 8 | # #### Example1 9 | 10 | # In[2]: 11 | 12 | 13 | from numpy import * 14 | from pylab import * 15 | x = linspace(-3, 3, 30) 16 | y = x**2 17 | plot(x, y) 18 | show() 19 | 20 | 21 | # #### Example2 22 | 23 | # In[3]: 24 | 25 | 26 | from pylab import * 27 | x = linspace(-3, 3, 30) 28 | y = x**2 29 | plot(x, y, 'r.') 30 | show() 31 | 32 | 33 | # #### Example3 34 | 35 | # In[4]: 36 | 37 | 38 | from pylab import * 39 | plot(x, sin(x)) 40 | plot(x, cos(x), 'r-') 41 | plot(x, -sin(x), 'g--') 42 | show() 43 | 44 | 45 | # In[ ]: 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.3-Figure and axe class.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[2]: 5 | 6 | 7 | import matplotlib.pyplot as plt 8 | y = [1, 4, 9, 16, 25,36,49, 64] 9 | x1 = [1, 16, 30, 42,55, 68, 77,88] 10 | x2 = [1,6,12,18,28, 40, 52, 65] 11 | fig = plt.figure() 12 | ax = fig.add_axes([0,0,1,1]) 13 | l1 = ax.plot(x1,y,'ys-') # solid line with yellow colour and square marker 14 | l2 = ax.plot(x2,y,'go--') # dash line with green colour and circle marker 15 | ax.legend(labels = ('tv', 'Smartphone'), loc = 'lower right') # legend placed at lower right 16 | ax.set_title("Advertisement effect on sales") 17 | ax.set_xlabel('medium') 18 | ax.set_ylabel('sales') 19 | plt.show() 20 | 21 | 22 | # In[ ]: 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.4-Subplots.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Subplots 5 | 6 | # In[1]: 7 | 8 | 9 | import matplotlib.pyplot as plt 10 | fig,a = plt.subplots(2,2) 11 | import numpy as np 12 | x = np.arange(1,5) 13 | a[0][0].plot(x,x*x) 14 | a[0][0].set_title('square') 15 | a[0][1].plot(x,np.sqrt(x)) 16 | a[0][1].set_title('square root') 17 | a[1][0].plot(x,np.exp(x)) 18 | a[1][0].set_title('exp') 19 | a[1][1].plot(x,np.log10(x)) 20 | a[1][1].set_title('log') 21 | plt.show() 22 | 23 | 24 | # ### Subplot2grid 25 | 26 | # In[2]: 27 | 28 | 29 | import matplotlib.pyplot as plt 30 | a1 = plt.subplot2grid((3,3),(0,0),colspan = 2) 31 | a2 = plt.subplot2grid((3,3),(0,2), rowspan = 3) 32 | a3 = plt.subplot2grid((3,3),(1,0),rowspan = 2, colspan = 2) 33 | import numpy as np 34 | x = np.arange(1,10) 35 | a2.plot(x, x*x) 36 | a2.set_title('square') 37 | a1.plot(x, np.exp(x)) 38 | a1.set_title('exp') 39 | a3.plot(x, np.log(x)) 40 | a3.set_title('log') 41 | plt.tight_layout() 42 | plt.show() 43 | 44 | 45 | # In[ ]: 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.5-Type of charts.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Matplotlib - Pie Chart 5 | 6 | # In[1]: 7 | 8 | 9 | from matplotlib import pyplot as plt 10 | import numpy as np 11 | fig = plt.figure() 12 | ax = fig.add_axes([0,0,1,1]) 13 | ax.axis('equal') 14 | langs = ['C', 'C++', 'Java', 'Python', 'PHP'] 15 | students = [23,17,35,29,12] 16 | ax.pie(students, labels = langs,autopct='%1.2f%%') 17 | plt.show() 18 | 19 | 20 | # ### Quiver Plot 21 | 22 | # In[2]: 23 | 24 | 25 | import matplotlib.pyplot as plt 26 | import numpy as np 27 | x,y = np.meshgrid(np.arange(-2, 2, .2), np.arange(-2, 2, .25)) 28 | z = x*np.exp(-x**2 - y**2) 29 | v, u = np.gradient(z, .2, .2) 30 | fig, ax = plt.subplots() 31 | q = ax.quiver(x,y,u,v) 32 | plt.show() 33 | 34 | 35 | # ### Violin Plot 36 | 37 | # In[3]: 38 | 39 | 40 | import matplotlib.pyplot as plt 41 | 42 | np.random.seed(10) 43 | collectn_1 = np.random.normal(100, 10, 200) 44 | collectn_2 = np.random.normal(80, 30, 200) 45 | collectn_3 = np.random.normal(90, 20, 200) 46 | collectn_4 = np.random.normal(70, 25, 200) 47 | 48 | ## combine these different collections into a list 49 | data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4] 50 | 51 | # Create a figure instance 52 | fig = plt.figure() 53 | 54 | # Create an axes instance 55 | ax = fig.add_axes([0,0,1,1]) 56 | 57 | # Create the boxplot 58 | bp = ax.violinplot(data_to_plot) 59 | plt.show() 60 | 61 | 62 | # In[ ]: 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.6-One-Dimensional Plots.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## One-Dimentional plots 5 | 6 | # ### Bar Plot 7 | 8 | # #### Example1 9 | 10 | # In[14]: 11 | 12 | 13 | import matplotlib.pyplot as plt 14 | fig = plt.figure() 15 | ax = fig.add_axes([0,0,1,1]) 16 | langs = ['C', 'C++', 'Java', 'Python', 'PHP'] 17 | students = [23,17,35,29,12] 18 | ax.bar(langs,students) 19 | plt.show() 20 | 21 | 22 | # #### Example2 23 | 24 | # In[15]: 25 | 26 | 27 | import numpy as np 28 | import matplotlib.pyplot as plt 29 | data = [[30, 25, 50, 20], 30 | [40, 23, 51, 17], 31 | [35, 22, 45, 19]] 32 | X = np.arange(4) 33 | fig = plt.figure() 34 | ax = fig.add_axes([0,0,1,1]) 35 | ax.bar(X + 0.00, data[0], color = 'b', width = 0.25) 36 | ax.bar(X + 0.25, data[1], color = 'g', width = 0.25) 37 | ax.bar(X + 0.50, data[2], color = 'r', width = 0.25) 38 | 39 | 40 | # #### Example3 41 | 42 | # In[16]: 43 | 44 | 45 | import numpy as np 46 | import matplotlib.pyplot as plt 47 | N = 5 48 | menMeans = (20, 35, 30, 35, 27) 49 | womenMeans = (25, 32, 34, 20, 25) 50 | ind = np.arange(N) # the x locations for the groups 51 | width = 0.35 52 | fig = plt.figure() 53 | ax = fig.add_axes([0,0,1,1]) 54 | ax.bar(ind, menMeans, width, color='r') 55 | ax.bar(ind, womenMeans, width,bottom=menMeans, color='b') 56 | ax.set_ylabel('Scores') 57 | ax.set_title('Scores by group and gender') 58 | ax.set_xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5')) 59 | ax.set_yticks(np.arange(0, 81, 10)) 60 | ax.legend(labels=['Men', 'Women']) 61 | plt.show() 62 | 63 | 64 | # ### Histogram 65 | 66 | # In[17]: 67 | 68 | 69 | from matplotlib import pyplot as plt 70 | import numpy as np 71 | fig,ax = plt.subplots(1,1) 72 | a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) 73 | ax.hist(a, bins = [0,25,50,75,100]) 74 | ax.set_title("histogram of result") 75 | ax.set_xticks([0,25,50,75,100]) 76 | ax.set_xlabel('marks') 77 | ax.set_ylabel('no. of students') 78 | plt.show() 79 | 80 | 81 | # ### Box Plot 82 | 83 | # In[18]: 84 | 85 | 86 | np.random.seed(10) 87 | collectn_1 = np.random.normal(100, 10, 200) 88 | collectn_2 = np.random.normal(80, 30, 200) 89 | collectn_3 = np.random.normal(90, 20, 200) 90 | collectn_4 = np.random.normal(70, 25, 200) 91 | 92 | 93 | # In[23]: 94 | 95 | 96 | ig = plt.figure() 97 | # Create an axes instance 98 | ax = fig.add_axes([0,0,1,1]) 99 | # Create the boxplot 100 | bp = ax.boxplot(data_to_plot) 101 | plt.show() 102 | 103 | 104 | # ### Density Plot 105 | 106 | # In[6]: 107 | 108 | 109 | from matplotlib import pyplot 110 | from pandas import read_csv 111 | path = r"diabetes.csv" 112 | names = ['plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] 113 | data = read_csv(path, names=names) 114 | data.plot(kind='density', subplots=True, layout=(3,3), sharex=False) 115 | pyplot.show() 116 | 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.7-Two-dimensional plots.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Creating two-dimensional plots of functions and data 5 | 6 | # #### Example1 7 | 8 | # In[1]: 9 | 10 | 11 | get_ipython().run_line_magic('matplotlib', 'inline') 12 | import numpy as np 13 | import matplotlib.pyplot as plt 14 | 15 | 16 | # In[2]: 17 | 18 | 19 | xvalues = np.linspace(-np.pi, np.pi) 20 | yvalues1 = np.sin(xvalues) 21 | yvalues2 = np.cos(xvalues) 22 | plt.plot(xvalues, yvalues1, lw=2, color='red', 23 | label='sin(x)') 24 | plt.plot(xvalues, yvalues2, lw=2, color='blue', 25 | label='cos(x)') 26 | plt.title('Trigonometric Functions') 27 | plt.xlabel('x') 28 | plt.ylabel('sin(x), cos(x)') 29 | plt.axhline(0, lw=0.5, color='black') 30 | plt.axvline(0, lw=0.5, color='black') 31 | plt.legend() 32 | None 33 | 34 | 35 | # ### Generating multiple plots in a single figure 36 | 37 | # In[3]: 38 | 39 | 40 | get_ipython().run_line_magic('matplotlib', 'inline') 41 | import numpy as np 42 | import matplotlib.pyplot as plt 43 | 44 | 45 | # In[4]: 46 | 47 | 48 | plt.figure(figsize=(6,6)) 49 | xvalues = np.linspace(-2, 2, 100) 50 | plt.subplot(2, 2, 1) 51 | yvalues = xvalues 52 | plt.plot(xvalues, yvalues, color='blue') 53 | plt.xlabel('$x$') 54 | plt.ylabel('$x$') 55 | plt.subplot(2, 2, 2) 56 | yvalues = xvalues ** 2 57 | plt.plot(xvalues, yvalues, color='green') 58 | plt.xlabel('$x$') 59 | plt.ylabel('$x^2$') 60 | plt.subplot(2, 2, 3) 61 | yvalues = xvalues ** 3 62 | plt.plot(xvalues, yvalues, color='red') 63 | plt.xlabel('$x$') 64 | plt.ylabel('$x^3$') 65 | plt.subplot(2, 2, 4) 66 | yvalues = xvalues ** 4 67 | plt.plot(xvalues, yvalues, color='black') 68 | plt.xlabel('$x$') 69 | plt.ylabel('$x^3$') 70 | plt.suptitle('Polynomial Functions') 71 | plt.tight_layout() 72 | plt.subplots_adjust(top=0.90) 73 | None 74 | 75 | 76 | # In[ ]: 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.8-Three-dimensional Plotting.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## Three-dimensional Plotting 5 | 6 | # ### Empty Plot 7 | 8 | # In[3]: 9 | 10 | 11 | from mpl_toolkits import mplot3d 12 | 13 | 14 | # In[4]: 15 | 16 | 17 | get_ipython().run_line_magic('matplotlib', 'inline') 18 | import numpy as np 19 | import matplotlib.pyplot as plt 20 | 21 | 22 | # In[5]: 23 | 24 | 25 | fig = plt.figure() 26 | ax = plt.axes(projection='3d') 27 | 28 | 29 | # ### 3D line plot 30 | 31 | # In[6]: 32 | 33 | 34 | from mpl_toolkits import mplot3d 35 | import numpy as np 36 | import matplotlib.pyplot as plt 37 | fig = plt.figure() 38 | ax = plt.axes(projection='3d') 39 | z = np.linspace(0, 1, 100) 40 | x = z * np.sin(20 * z) 41 | y = z * np.cos(20 * z) 42 | ax.plot3D(x, y, z, 'gray') 43 | ax.set_title('3D line plot') 44 | plt.show() 45 | 46 | 47 | # ### 3D scatter plot 48 | 49 | # In[7]: 50 | 51 | 52 | from mpl_toolkits import mplot3d 53 | import numpy as np 54 | import matplotlib.pyplot as plt 55 | fig = plt.figure() 56 | ax = plt.axes(projection='3d') 57 | z = np.linspace(0, 1, 100) 58 | x = z * np.sin(20 * z) 59 | y = z * np.cos(20 * z) 60 | c = x + y 61 | ax.scatter(x, y, z, c=c) 62 | ax.set_title('3d Scatter plot') 63 | plt.show() 64 | 65 | 66 | # ### 3D Contour Plot 67 | 68 | # In[3]: 69 | 70 | 71 | from mpl_toolkits import mplot3d 72 | import numpy as np 73 | import matplotlib.pyplot as plt 74 | def f(x, y): 75 | return np.sin(np.sqrt(x ** 2 + y ** 2)) 76 | 77 | x = np.linspace(-6, 6, 30) 78 | y = np.linspace(-6, 6, 30) 79 | 80 | X, Y = np.meshgrid(x, y) 81 | Z = f(X, Y) 82 | 83 | fig = plt.figure() 84 | ax = plt.axes(projection='3d') 85 | ax.contour3D(X, Y, Z, 50, cmap='binary') 86 | ax.set_xlabel('x') 87 | ax.set_ylabel('y') 88 | ax.set_zlabel('z') 89 | ax.set_title('3D contour') 90 | plt.show() 91 | 92 | 93 | # In[10]: 94 | 95 | 96 | from mpl_toolkits import mplot3d 97 | import numpy as np 98 | import matplotlib.pyplot as plt 99 | def f(x, y): 100 | return np.sin(np.sqrt(x ** 2 + y ** 2)) 101 | 102 | x = np.linspace(-6, 6, 30) 103 | y = np.linspace(-6, 6, 30) 104 | 105 | X, Y = np.meshgrid(x, y) 106 | Z = f(X, Y) 107 | 108 | ax = plt.axes(projection='3d') 109 | ax.contour3D(X, Y, Z, 50, cmap='binary') 110 | ax.set_xlabel('x') 111 | ax.set_ylabel('y') 112 | ax.set_zlabel('z') 113 | ax.view_init(60, 35) 114 | fig 115 | 116 | 117 | # ### 3D Wireframe plot 118 | # 119 | 120 | # In[5]: 121 | 122 | 123 | from mpl_toolkits import mplot3d 124 | import numpy as np 125 | import matplotlib.pyplot as plt 126 | def f(x, y): 127 | return np.sin(np.sqrt(x ** 2 + y ** 2)) 128 | 129 | x = np.linspace(-6, 6, 30) 130 | y = np.linspace(-6, 6, 30) 131 | 132 | X, Y = np.meshgrid(x, y) 133 | Z = f(X, Y) 134 | 135 | fig = plt.figure() 136 | ax = plt.axes(projection='3d') 137 | ax.plot_wireframe(X, Y, Z, color='black') 138 | ax.set_title('wireframe') 139 | plt.show() 140 | 141 | 142 | # ### 3D Surface plot 143 | 144 | # In[6]: 145 | 146 | 147 | from mpl_toolkits import mplot3d 148 | import numpy as np 149 | import matplotlib.pyplot as plt 150 | x = np.outer(np.linspace(-2, 2, 30), np.ones(30)) 151 | y = x.copy().T # transpose 152 | z = np.cos(x ** 2 + y ** 2) 153 | 154 | fig = plt.figure() 155 | ax = plt.axes(projection='3d') 156 | 157 | ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none') 158 | ax.set_title('Surface plot') 159 | plt.show() 160 | 161 | 162 | # In[11]: 163 | 164 | 165 | r = np.linspace(0, 6, 20) 166 | theta = np.linspace(-0.9 * np.pi, 0.8 * np.pi, 40) 167 | r, theta = np.meshgrid(r, theta) 168 | 169 | X = r * np.sin(theta) 170 | Y = r * np.cos(theta) 171 | Z = f(X, Y) 172 | 173 | ax = plt.axes(projection='3d') 174 | ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 175 | cmap='viridis', edgecolor='none'); 176 | 177 | 178 | # In[ ]: 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/4.9-Working with Images.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Working with Images 5 | 6 | # In[14]: 7 | 8 | 9 | import matplotlib.pyplot as plt 10 | import matplotlib.image as mpimg 11 | import numpy as np 12 | img = mpimg.imread('matplotlib.png') 13 | 14 | 15 | # In[15]: 16 | 17 | 18 | plt.imsave("matplotlib.png", img, cmap = 'gray', origin = 'lower') 19 | 20 | 21 | # In[16]: 22 | 23 | 24 | imgplot = plt.imshow(img) 25 | 26 | 27 | # In[ ]: 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Code_Scripts/txt.: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/3.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/4.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.0-Jupyter notebook/5.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.1-PyplotAPI/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.1-PyplotAPI/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.1-PyplotAPI/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.1-PyplotAPI/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.1-PyplotAPI/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.1-PyplotAPI/3.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.2-Pylab module/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.2-Pylab module/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.2-Pylab module/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.2-Pylab module/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.2-Pylab module/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.2-Pylab module/3.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.3-Figure and axe class/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.3-Figure and axe class/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.4-Subplots/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.4-Subplots/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.4-Subplots/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.4-Subplots/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.5-Types Of Charts/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.5-Types Of Charts/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.5-Types Of Charts/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.5-Types Of Charts/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.5-Types Of Charts/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.5-Types Of Charts/3.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.5-Types Of Charts/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.5-Types Of Charts/4.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.5-Types Of Charts/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.5-Types Of Charts/5.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/3.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/4.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/5.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/6.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.6-Onedimensional plots/7.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.7-Twodimensional plots/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.7-Twodimensional plots/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.7-Twodimensional plots/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.7-Twodimensional plots/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/2.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/3.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/4.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/5.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/6.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/7.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.8-Three dimensional plots/8.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.9-Working with images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.9-Working with images/1.jpg -------------------------------------------------------------------------------- /Volume III/4-Matplotlib/Images/4.9-Working with images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/4-Matplotlib/Images/4.9-Working with images/2.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Code_Scripts/5.0-first seaborn plot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Importing Libraries 5 | 6 | # In[4]: 7 | 8 | 9 | get_ipython().run_line_magic('matplotlib', 'inline') 10 | import matplotlib.pyplot as plt 11 | import seaborn as sns 12 | 13 | 14 | # In[5]: 15 | 16 | 17 | df = sns.load_dataset("penguins") 18 | sns.pairplot(df, hue="species") 19 | 20 | 21 | # ### Apply the default seaborn theme, scaling, and color palette 22 | 23 | # In[6]: 24 | 25 | 26 | # Plot color palette 27 | def plot_color_palette(palette: str): 28 | figure = sns.palplot(sns.color_palette()) 29 | plt.xlabel("Color palette: " + palette) 30 | plt.show(figure) 31 | 32 | palettes = ["deep", "muted", "pastel", "bright", "dark", "colorblind"] 33 | for palette in palettes: 34 | sns.set(palette=palette) 35 | plot_color_palette(palette) 36 | 37 | 38 | # ### Load your dataset 39 | 40 | # In[7]: 41 | 42 | 43 | tips = sns.load_dataset("tips") 44 | tips.head() 45 | 46 | 47 | # ### Plot data 48 | 49 | # In[9]: 50 | 51 | 52 | # Import seaborn 53 | import seaborn as sns 54 | 55 | # Apply the default theme 56 | sns.set_style() 57 | 58 | # Load an example dataset 59 | tips = sns.load_dataset("tips") 60 | 61 | # Create a visualization 62 | sns.relplot(x="total_bill", y="tip", 63 | col="time", # Categorical variables that will determine the faceting of the grid. 64 | hue="smoker", # Grouping variable that will produce elements with different colors. 65 | style="smoker", # Grouping variable that will produce elements with different styles. 66 | size="size", # Grouping variable that will produce elements with different sizes. 67 | data=tips) 68 | 69 | 70 | # ### Example1 71 | 72 | # In[11]: 73 | 74 | 75 | sns.lmplot(x="total_bill", y="tip", data=tips) 76 | 77 | 78 | # ### Example2 79 | 80 | # In[12]: 81 | 82 | 83 | sns.lmplot(x="size", y="tip", data=tips) 84 | 85 | 86 | # ### Example3 87 | 88 | # In[13]: 89 | 90 | 91 | import numpy as np 92 | sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean) 93 | 94 | 95 | # ### Example4 96 | # 97 | 98 | # In[14]: 99 | 100 | 101 | tips["big_tip"] = (tips.tip / tips.total_bill) > .15 102 | sns.lmplot(x="total_bill", y="big_tip", 103 | y_jitter=.03, 104 | logistic=True, 105 | data=tips) 106 | 107 | 108 | # ### Example5 109 | 110 | # In[15]: 111 | 112 | 113 | sns.jointplot(x="total_bill", y="tip", 114 | kind="reg", # Options are "scatter", "reg", "resid", "kde", "hex" 115 | data=tips) 116 | 117 | 118 | # ### Example6 119 | 120 | # In[16]: 121 | 122 | 123 | sns.pairplot(hue="smoker", # Grouping variable that will produce elements with different colors. 124 | data=tips) 125 | 126 | 127 | # ## API abstraction across visualizations 128 | 129 | # In[11]: 130 | 131 | 132 | dots = sns.load_dataset("dots") 133 | sns.relplot( 134 | data=dots, kind="line", 135 | x="time", y="firing_rate", col="align", 136 | hue="choice", size="coherence", style="choice", 137 | facet_kws=dict(sharex=False), 138 | ) 139 | 140 | 141 | # ## Statistical estimation and error bars 142 | 143 | # In[12]: 144 | 145 | 146 | fmri = sns.load_dataset("fmri") 147 | sns.relplot( 148 | data=fmri, kind="line", 149 | x="timepoint", y="signal", col="region", 150 | hue="event", style="event", 151 | ) 152 | 153 | 154 | # In[13]: 155 | 156 | 157 | sns.lmplot(data=tips, x="total_bill", y="tip", col="time", hue="smoker") 158 | 159 | 160 | # ## Specialized plots for categorical data 161 | 162 | # ### Example1 163 | 164 | # In[14]: 165 | 166 | 167 | sns.catplot(x="day", y="total_bill", 168 | hue="smoker", # Grouping variable that will produce elements with different colors. 169 | kind="swarm", # Options are: "point", "bar", "strip", "swarm", "box", "violin", or "boxen" 170 | data=tips) 171 | 172 | 173 | # ### Example2 174 | 175 | # In[15]: 176 | 177 | 178 | sns.catplot(x="day", y="total_bill", 179 | hue="smoker", # Grouping variable that will produce elements with different colors. 180 | kind="violin", # Options are: "point", "bar", "strip", "swarm", "box", "violin", or "boxen" 181 | split=True, 182 | data=tips) 183 | 184 | 185 | # ### Example3 186 | 187 | # In[16]: 188 | 189 | 190 | sns.catplot(x="day", y="total_bill", 191 | hue="smoker", # Grouping variable that will produce elements with different colors. 192 | kind="bar", # Options are: "point", "bar", "strip", "swarm", "box", "violin", or "boxen" 193 | data=tips) 194 | 195 | 196 | # ### Opinionated defaults and flexible customization 197 | 198 | # In[18]: 199 | 200 | 201 | sns.relplot( 202 | data=df, 203 | x="bill_length_mm", y="bill_depth_mm", hue="body_mass_g" 204 | ) 205 | 206 | 207 | # In[ ]: 208 | 209 | 210 | 211 | 212 | -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Code_Scripts/5.1-Seaborn Styling,Figure Style and Scale.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## Customizing the Overall Look of Your Figure 5 | 6 | # ### Built-in Themes 7 | 8 | # In[1]: 9 | 10 | 11 | get_ipython().run_line_magic('matplotlib', 'inline') 12 | import seaborn as sns 13 | 14 | 15 | # In[2]: 16 | 17 | 18 | tips = sns.load_dataset("tips") 19 | 20 | 21 | # In[3]: 22 | 23 | 24 | sns.set_style("darkgrid") 25 | sns.stripplot(x="day", y="total_bill", data=tips) 26 | 27 | 28 | # ### Background Color 29 | 30 | # In[4]: 31 | 32 | 33 | sns.set_style("dark") 34 | sns.stripplot(x="day", y="total_bill", data=tips) 35 | 36 | 37 | # In[5]: 38 | 39 | 40 | sns.set_style("ticks") 41 | sns.stripplot(x="day", y="total_bill", data=tips) 42 | 43 | 44 | # ### Grids 45 | 46 | # In[6]: 47 | 48 | 49 | sns.set_style("whitegrid") 50 | sns.stripplot(x="day", y="total_bill", data=tips) 51 | 52 | 53 | # In[7]: 54 | 55 | 56 | sns.set_style("white") 57 | sns.stripplot(x="day", y="total_bill", data=tips) 58 | 59 | 60 | # ### Despine 61 | 62 | # In[8]: 63 | 64 | 65 | sns.set_style("white") 66 | sns.stripplot(x="day", y="total_bill", data=tips) 67 | sns.despine() 68 | 69 | 70 | # In[9]: 71 | 72 | 73 | sns.set_style("whitegrid") 74 | sns.stripplot(x="day", y="total_bill", data=tips) 75 | sns.despine(left=True, bottom=True) 76 | 77 | 78 | # ## Scaling Figure Styles for Different Mediums 79 | 80 | # ### Scaling Plots 81 | 82 | # In[10]: 83 | 84 | 85 | sns.set_style("ticks") 86 | 87 | # Smallest context: 88 | sns.set_context("paper") 89 | sns.stripplot(x="day", y="total_bill", data=tips) 90 | 91 | 92 | # In[11]: 93 | 94 | 95 | sns.set_style("ticks") 96 | 97 | # Largest Context: 98 | sns.set_context("poster") 99 | sns.stripplot(x="day", y="total_bill", data=tips) 100 | 101 | 102 | # ### Scaling Fonts and Line Widths 103 | 104 | # In[12]: 105 | 106 | 107 | # Set font scale and reduce grid line width to match 108 | sns.set_context("poster", font_scale = .5, rc={"grid.linewidth": 0.6}) 109 | sns.stripplot(x="day", y="total_bill", data=tips) 110 | 111 | 112 | # In[13]: 113 | 114 | 115 | # Set font scale and increase grid line width to match 116 | sns.set_context("poster", font_scale = 1, rc={"grid.linewidth": 5}) 117 | sns.stripplot(x="day", y="total_bill", data=tips) 118 | 119 | 120 | # ### The RC Parameter 121 | 122 | # In[14]: 123 | 124 | 125 | sns.set_style("ticks") 126 | sns.set_context("poster") 127 | sns.stripplot(x="day", y="total_bill", data=tips) 128 | sns.plotting_context() 129 | 130 | 131 | # ### How To Temporarily Set The Plot Style 132 | # 133 | 134 | # In[1]: 135 | 136 | 137 | # Import necessary libraries 138 | import matplotlib.pyplot as plt 139 | import seaborn as sns 140 | 141 | # Load data 142 | iris = sns.load_dataset("iris") 143 | tips = sns.load_dataset("tips") 144 | 145 | # Set axes style to white for first subplot 146 | with sns.axes_style("white"): 147 | plt.subplot(211) 148 | sns.swarmplot(x="species", y="petal_length", data=iris) 149 | 150 | # Initialize second subplot 151 | plt.subplot(212) 152 | 153 | # Plot violinplot 154 | sns.violinplot(x = "total_bill", data=tips) 155 | 156 | # Show the plot 157 | plt.show() 158 | 159 | 160 | # ### How To Set The Figure Size in Seaborn 161 | # 162 | 163 | # In[2]: 164 | 165 | 166 | # Import necessary libraries 167 | import seaborn as sns 168 | import matplotlib.pyplot as plt 169 | 170 | # Initialize Figure and Axes object 171 | fig, ax = plt.subplots(figsize=(10,4)) 172 | 173 | # Load in the data 174 | iris = sns.load_dataset("iris") 175 | 176 | # Create swarmplot 177 | sns.swarmplot(x="species", y="petal_length", data=iris, ax=ax) 178 | 179 | # Show plot 180 | plt.show() 181 | 182 | 183 | # In[3]: 184 | 185 | 186 | # Import the libraries 187 | import matplotlib.pyplot as plt 188 | import seaborn as sns 189 | 190 | # Load data 191 | titanic = sns.load_dataset("titanic") 192 | 193 | # Set up a factorplot 194 | g = sns.factorplot("class", "survived", "sex", data=titanic, kind="bar", size=6, aspect=2, palette="muted", legend=False) 195 | 196 | # Show plot 197 | plt.show() 198 | 199 | 200 | # In[ ]: 201 | 202 | 203 | 204 | 205 | -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Code_Scripts/5.2-Plot varieties/Pie Chart & Bar Chart.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## 2. Pie Chart & Bar Chart 5 | 6 | # In[16]: 7 | 8 | 9 | import pandas as pd 10 | import numpy as np 11 | pstore = pd.read_csv("googleplaystore.csv") 12 | 13 | 14 | # In[17]: 15 | 16 | 17 | #importing all the libraries 18 | import numpy as np 19 | import pandas as pd 20 | import matplotlib.pyplot as plt 21 | import seaborn as sns 22 | 23 | #Analyzing the Content Rating column 24 | pstore['Content Rating'].value_counts() 25 | 26 | 27 | # In[18]: 28 | 29 | 30 | #importing all the libraries 31 | import numpy as np 32 | import pandas as pd 33 | import matplotlib.pyplot as plt 34 | import seaborn as sns 35 | 36 | #Remove the rows with values which are less represented 37 | pstore = pstore[~pstore['Content Rating'].isin(["Adults only 18+","Unrated"])] 38 | 39 | #Resetting the index 40 | pstore.reset_index(inplace=True, drop=True) 41 | 42 | #Analyzing the Content Rating column again 43 | pstore['Content Rating'].value_counts() 44 | 45 | 46 | # ### Pie Chart 47 | 48 | # In[19]: 49 | 50 | 51 | #importing all the libraries 52 | import numpy as np 53 | import pandas as pd 54 | import matplotlib.pyplot as plt 55 | import seaborn as sns 56 | 57 | #Plotting a pie chart 58 | plt.figure(figsize=[9,7]) 59 | pstore['Content Rating'].value_counts().plot.pie() 60 | plt.show() 61 | 62 | 63 | # ### Bar Chart 64 | 65 | # In[20]: 66 | 67 | 68 | #importing all the libraries 69 | import numpy as np 70 | import pandas as pd 71 | import matplotlib.pyplot as plt 72 | import seaborn as sns 73 | 74 | #Plotting a bar chart 75 | plt.figure(figsize=[9,7]) 76 | pstore['Content Rating'].value_counts().plot.barh() 77 | plt.show() 78 | 79 | 80 | # ## 3. Scatter Plots 81 | # 82 | 83 | # In[4]: 84 | 85 | 86 | import seaborn as sns 87 | tips = sns.load_dataset("tips") 88 | 89 | 90 | # In[5]: 91 | 92 | 93 | sns.scatterplot(data=tips, x="total_bill", y="tip") 94 | 95 | 96 | # In[6]: 97 | 98 | 99 | sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time") 100 | 101 | 102 | # In[8]: 103 | 104 | 105 | sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", style="time") 106 | 107 | 108 | # In[7]: 109 | 110 | 111 | sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day", style="time") 112 | 113 | 114 | # ## 4.Pair plot 115 | # 116 | 117 | # In[10]: 118 | 119 | 120 | import seaborn as sns 121 | 122 | sns.set(style="ticks", color_codes=True) 123 | iris = sns.load_dataset("iris") 124 | g = sns.pairplot(iris) 125 | 126 | import matplotlib.pyplot as plt 127 | plt.show() 128 | 129 | 130 | # In[11]: 131 | 132 | 133 | g = sns.pairplot(iris, vars=["sepal_width", "sepal_length"]) 134 | 135 | 136 | # In[12]: 137 | 138 | 139 | g = sns.pairplot(iris, diag_kind="kde") 140 | 141 | 142 | # ## 5. Heatmap 143 | 144 | # In[13]: 145 | 146 | 147 | import pandas as pd 148 | import numpy as np 149 | import seaborn as sns 150 | import matplotlib.pyplot as plt 151 | 152 | np.random.seed(0) 153 | sns.set() 154 | uniform_data = np.random.rand(10, 12) 155 | ax = sns.heatmap(uniform_data, vmin=0, vmax=1) 156 | plt.show() 157 | 158 | 159 | # ### heatmap colors 160 | 161 | # In[14]: 162 | 163 | 164 | import pandas as pd 165 | import numpy as np 166 | import seaborn as sns 167 | import matplotlib.pyplot as plt 168 | 169 | df = pd.DataFrame(np.random.randn(50).reshape(10,5)) 170 | corr = df.corr() 171 | 172 | ax1 = sns.heatmap(corr, cbar=0, linewidths=2,vmax=1, vmin=0, square=True, cmap='Blues') 173 | plt.show() 174 | 175 | 176 | # ### heatmap data 177 | 178 | # In[15]: 179 | 180 | 181 | import pandas as pd 182 | import numpy as np 183 | import seaborn as sns 184 | import matplotlib.pyplot as plt 185 | 186 | sns.set() 187 | flights = sns.load_dataset("flights") 188 | flights = flights.pivot("month", "year", "passengers") 189 | ax = sns.heatmap(flights) 190 | plt.title("Heatmap Flight Data") 191 | plt.show() 192 | 193 | 194 | # In[ ]: 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Code_Scripts/5.2-Plot varieties/Plot Varieties.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[30]: 5 | 6 | 7 | import seaborn as sns 8 | 9 | 10 | # ## 1. Distribution Plots 11 | 12 | # In[31]: 13 | 14 | 15 | import pandas as pd 16 | import numpy as np 17 | pstore = pd.read_csv("googleplaystore.csv") 18 | pstore.head(10) 19 | 20 | 21 | # In[32]: 22 | 23 | 24 | #importing all the libraries 25 | import numpy as np 26 | import pandas as pd 27 | import matplotlib.pyplot as plt 28 | import seaborn as sns 29 | 30 | #Create a distribution plot for rating 31 | sns.distplot(pstore.Rating) 32 | plt.show() 33 | 34 | 35 | # In[33]: 36 | 37 | 38 | #Change the number of bins 39 | sns.distplot(pstore.Rating, bins=20, kde = False) 40 | plt.show() 41 | 42 | 43 | # ### distribution of App ratings 44 | 45 | # In[34]: 46 | 47 | 48 | #importing all the libraries 49 | import numpy as np 50 | import pandas as pd 51 | import matplotlib.pyplot as plt 52 | import seaborn as sns 53 | 54 | #Create a distribution plot for rating 55 | sns.distplot(pstore.Rating, bins=20, color="g") 56 | plt.title("Distribution of app ratings", fontsize=20, color = 'red') 57 | plt.show() 58 | 59 | 60 | # ### Styling the Seaborn graphs 61 | 62 | # In[35]: 63 | 64 | 65 | #importing all the libraries 66 | import numpy as np 67 | import pandas as pd 68 | import matplotlib.pyplot as plt 69 | import seaborn as sns 70 | 71 | 72 | #Adding dark background to the graph 73 | plt.style.use("dark_background") 74 | #Create a distribution plot for rating 75 | sns.distplot(pstore.Rating, bins=20, color="g") 76 | plt.title("Distribution of app ratings", fontsize=20, color = 'red') 77 | plt.show() 78 | 79 | -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Code_Scripts/5.2-Plot varieties/iris.csv: -------------------------------------------------------------------------------- 1 | "sepal.length","sepal.width","petal.length","petal.width","variety" 2 | 5.1,3.5,1.4,.2,"Setosa" 3 | 4.9,3,1.4,.2,"Setosa" 4 | 4.7,3.2,1.3,.2,"Setosa" 5 | 4.6,3.1,1.5,.2,"Setosa" 6 | 5,3.6,1.4,.2,"Setosa" 7 | 5.4,3.9,1.7,.4,"Setosa" 8 | 4.6,3.4,1.4,.3,"Setosa" 9 | 5,3.4,1.5,.2,"Setosa" 10 | 4.4,2.9,1.4,.2,"Setosa" 11 | 4.9,3.1,1.5,.1,"Setosa" 12 | 5.4,3.7,1.5,.2,"Setosa" 13 | 4.8,3.4,1.6,.2,"Setosa" 14 | 4.8,3,1.4,.1,"Setosa" 15 | 4.3,3,1.1,.1,"Setosa" 16 | 5.8,4,1.2,.2,"Setosa" 17 | 5.7,4.4,1.5,.4,"Setosa" 18 | 5.4,3.9,1.3,.4,"Setosa" 19 | 5.1,3.5,1.4,.3,"Setosa" 20 | 5.7,3.8,1.7,.3,"Setosa" 21 | 5.1,3.8,1.5,.3,"Setosa" 22 | 5.4,3.4,1.7,.2,"Setosa" 23 | 5.1,3.7,1.5,.4,"Setosa" 24 | 4.6,3.6,1,.2,"Setosa" 25 | 5.1,3.3,1.7,.5,"Setosa" 26 | 4.8,3.4,1.9,.2,"Setosa" 27 | 5,3,1.6,.2,"Setosa" 28 | 5,3.4,1.6,.4,"Setosa" 29 | 5.2,3.5,1.5,.2,"Setosa" 30 | 5.2,3.4,1.4,.2,"Setosa" 31 | 4.7,3.2,1.6,.2,"Setosa" 32 | 4.8,3.1,1.6,.2,"Setosa" 33 | 5.4,3.4,1.5,.4,"Setosa" 34 | 5.2,4.1,1.5,.1,"Setosa" 35 | 5.5,4.2,1.4,.2,"Setosa" 36 | 4.9,3.1,1.5,.2,"Setosa" 37 | 5,3.2,1.2,.2,"Setosa" 38 | 5.5,3.5,1.3,.2,"Setosa" 39 | 4.9,3.6,1.4,.1,"Setosa" 40 | 4.4,3,1.3,.2,"Setosa" 41 | 5.1,3.4,1.5,.2,"Setosa" 42 | 5,3.5,1.3,.3,"Setosa" 43 | 4.5,2.3,1.3,.3,"Setosa" 44 | 4.4,3.2,1.3,.2,"Setosa" 45 | 5,3.5,1.6,.6,"Setosa" 46 | 5.1,3.8,1.9,.4,"Setosa" 47 | 4.8,3,1.4,.3,"Setosa" 48 | 5.1,3.8,1.6,.2,"Setosa" 49 | 4.6,3.2,1.4,.2,"Setosa" 50 | 5.3,3.7,1.5,.2,"Setosa" 51 | 5,3.3,1.4,.2,"Setosa" 52 | 7,3.2,4.7,1.4,"Versicolor" 53 | 6.4,3.2,4.5,1.5,"Versicolor" 54 | 6.9,3.1,4.9,1.5,"Versicolor" 55 | 5.5,2.3,4,1.3,"Versicolor" 56 | 6.5,2.8,4.6,1.5,"Versicolor" 57 | 5.7,2.8,4.5,1.3,"Versicolor" 58 | 6.3,3.3,4.7,1.6,"Versicolor" 59 | 4.9,2.4,3.3,1,"Versicolor" 60 | 6.6,2.9,4.6,1.3,"Versicolor" 61 | 5.2,2.7,3.9,1.4,"Versicolor" 62 | 5,2,3.5,1,"Versicolor" 63 | 5.9,3,4.2,1.5,"Versicolor" 64 | 6,2.2,4,1,"Versicolor" 65 | 6.1,2.9,4.7,1.4,"Versicolor" 66 | 5.6,2.9,3.6,1.3,"Versicolor" 67 | 6.7,3.1,4.4,1.4,"Versicolor" 68 | 5.6,3,4.5,1.5,"Versicolor" 69 | 5.8,2.7,4.1,1,"Versicolor" 70 | 6.2,2.2,4.5,1.5,"Versicolor" 71 | 5.6,2.5,3.9,1.1,"Versicolor" 72 | 5.9,3.2,4.8,1.8,"Versicolor" 73 | 6.1,2.8,4,1.3,"Versicolor" 74 | 6.3,2.5,4.9,1.5,"Versicolor" 75 | 6.1,2.8,4.7,1.2,"Versicolor" 76 | 6.4,2.9,4.3,1.3,"Versicolor" 77 | 6.6,3,4.4,1.4,"Versicolor" 78 | 6.8,2.8,4.8,1.4,"Versicolor" 79 | 6.7,3,5,1.7,"Versicolor" 80 | 6,2.9,4.5,1.5,"Versicolor" 81 | 5.7,2.6,3.5,1,"Versicolor" 82 | 5.5,2.4,3.8,1.1,"Versicolor" 83 | 5.5,2.4,3.7,1,"Versicolor" 84 | 5.8,2.7,3.9,1.2,"Versicolor" 85 | 6,2.7,5.1,1.6,"Versicolor" 86 | 5.4,3,4.5,1.5,"Versicolor" 87 | 6,3.4,4.5,1.6,"Versicolor" 88 | 6.7,3.1,4.7,1.5,"Versicolor" 89 | 6.3,2.3,4.4,1.3,"Versicolor" 90 | 5.6,3,4.1,1.3,"Versicolor" 91 | 5.5,2.5,4,1.3,"Versicolor" 92 | 5.5,2.6,4.4,1.2,"Versicolor" 93 | 6.1,3,4.6,1.4,"Versicolor" 94 | 5.8,2.6,4,1.2,"Versicolor" 95 | 5,2.3,3.3,1,"Versicolor" 96 | 5.6,2.7,4.2,1.3,"Versicolor" 97 | 5.7,3,4.2,1.2,"Versicolor" 98 | 5.7,2.9,4.2,1.3,"Versicolor" 99 | 6.2,2.9,4.3,1.3,"Versicolor" 100 | 5.1,2.5,3,1.1,"Versicolor" 101 | 5.7,2.8,4.1,1.3,"Versicolor" 102 | 6.3,3.3,6,2.5,"Virginica" 103 | 5.8,2.7,5.1,1.9,"Virginica" 104 | 7.1,3,5.9,2.1,"Virginica" 105 | 6.3,2.9,5.6,1.8,"Virginica" 106 | 6.5,3,5.8,2.2,"Virginica" 107 | 7.6,3,6.6,2.1,"Virginica" 108 | 4.9,2.5,4.5,1.7,"Virginica" 109 | 7.3,2.9,6.3,1.8,"Virginica" 110 | 6.7,2.5,5.8,1.8,"Virginica" 111 | 7.2,3.6,6.1,2.5,"Virginica" 112 | 6.5,3.2,5.1,2,"Virginica" 113 | 6.4,2.7,5.3,1.9,"Virginica" 114 | 6.8,3,5.5,2.1,"Virginica" 115 | 5.7,2.5,5,2,"Virginica" 116 | 5.8,2.8,5.1,2.4,"Virginica" 117 | 6.4,3.2,5.3,2.3,"Virginica" 118 | 6.5,3,5.5,1.8,"Virginica" 119 | 7.7,3.8,6.7,2.2,"Virginica" 120 | 7.7,2.6,6.9,2.3,"Virginica" 121 | 6,2.2,5,1.5,"Virginica" 122 | 6.9,3.2,5.7,2.3,"Virginica" 123 | 5.6,2.8,4.9,2,"Virginica" 124 | 7.7,2.8,6.7,2,"Virginica" 125 | 6.3,2.7,4.9,1.8,"Virginica" 126 | 6.7,3.3,5.7,2.1,"Virginica" 127 | 7.2,3.2,6,1.8,"Virginica" 128 | 6.2,2.8,4.8,1.8,"Virginica" 129 | 6.1,3,4.9,1.8,"Virginica" 130 | 6.4,2.8,5.6,2.1,"Virginica" 131 | 7.2,3,5.8,1.6,"Virginica" 132 | 7.4,2.8,6.1,1.9,"Virginica" 133 | 7.9,3.8,6.4,2,"Virginica" 134 | 6.4,2.8,5.6,2.2,"Virginica" 135 | 6.3,2.8,5.1,1.5,"Virginica" 136 | 6.1,2.6,5.6,1.4,"Virginica" 137 | 7.7,3,6.1,2.3,"Virginica" 138 | 6.3,3.4,5.6,2.4,"Virginica" 139 | 6.4,3.1,5.5,1.8,"Virginica" 140 | 6,3,4.8,1.8,"Virginica" 141 | 6.9,3.1,5.4,2.1,"Virginica" 142 | 6.7,3.1,5.6,2.4,"Virginica" 143 | 6.9,3.1,5.1,2.3,"Virginica" 144 | 5.8,2.7,5.1,1.9,"Virginica" 145 | 6.8,3.2,5.9,2.3,"Virginica" 146 | 6.7,3.3,5.7,2.5,"Virginica" 147 | 6.7,3,5.2,2.3,"Virginica" 148 | 6.3,2.5,5,1.9,"Virginica" 149 | 6.5,3,5.2,2,"Virginica" 150 | 6.2,3.4,5.4,2.3,"Virginica" 151 | 5.9,3,5.1,1.8,"Virginica" -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/1.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/10.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/11.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/12.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/13.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/14.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/15.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/16.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/17.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/2.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/3.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/4.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/5.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/6.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/7.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/8.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.0-Introduction/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.0-Introduction/9.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/1.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/10.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/11.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/12.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/13.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/14.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/15.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/2.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/3.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/4.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/5.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/6.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/7.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/8.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.1-Styling and scaling/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.1-Styling and scaling/9.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/1.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/10.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/11.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/12.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/13.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/14.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/15.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/16.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/2.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/3.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/4.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/5.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/6.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/7.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/8.jpg -------------------------------------------------------------------------------- /Volume III/5-Seaborn/Images/5.2-Plot Varities/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/5-Seaborn/Images/5.2-Plot Varities/9.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Code_Scripts/6.1-Layers/london2018.csv: -------------------------------------------------------------------------------- 1 | Year,Month,Tmax,Tmin,Rain,Sun 2 | 2018,1,9.7,3.8,58,46.5 3 | 2018,2,6.7,0.6,29,92.0 4 | 2018,3,9.8,3,81.2,70.3 5 | 2018,4,15.5,7.9,65.2,113.4 6 | 2018,5,20.8,9.8,58.4,248.3 7 | 2018,6,24.2,13.1,0.4,234.5 8 | 2018,7,28.3,16.4,14.8,272.5 9 | 2018,8,24.5,14.5,48.2,182.1 10 | 2018,9,20.9,11,29.4,195.0 11 | 2018,10,16.5,8.5,61,137.0 12 | 2018,11,12.2,5.8,73.8,72.9 13 | 2018,12,10.7,5.2,60.6,40.3 -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/1.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/10.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/11.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/12.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/13.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/14.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/15.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/2.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/3.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/4.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/5.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/6.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/7.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/8.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.0-Introduction/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.0-Introduction/9.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/1.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/2.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/3.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/4.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/5.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/6.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/7.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/8.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/9.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/Table1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/Table1.jpg -------------------------------------------------------------------------------- /Volume III/6-ggplot/Images/6.1-Layers/Table2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume III/6-ggplot/Images/6.1-Layers/Table2.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.1-Ndarray Object .py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Example 1 5 | 6 | # In[2]: 7 | 8 | 9 | import numpy as np 10 | a = np.array([1,2,3]) 11 | print (a) 12 | 13 | 14 | # ### Example 2 15 | 16 | # In[4]: 17 | 18 | 19 | # more than one dimensions 20 | import numpy as np 21 | a = np.array([[1, 2], [3, 4]]) 22 | print (a) 23 | 24 | 25 | # ### Example 3 26 | 27 | # In[5]: 28 | 29 | 30 | # minimum dimensions 31 | import numpy as np 32 | a = np.array([1, 2, 3,4,5], ndmin = 2) 33 | print (a) 34 | 35 | 36 | # ### Example 4 37 | 38 | # In[7]: 39 | 40 | 41 | # dtype parameter 42 | import numpy as np 43 | a = np.array([1, 2, 3], dtype = complex) 44 | print (a) 45 | 46 | 47 | # ### Data Type Objects 48 | 49 | # ### Example 1 50 | 51 | # In[9]: 52 | 53 | 54 | # using array-scalar type 55 | import numpy as np 56 | dt = np.dtype(np.int32) 57 | print (dt) 58 | 59 | 60 | # ### Example 2 61 | 62 | # In[11]: 63 | 64 | 65 | #int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4', etc. 66 | import numpy as np 67 | 68 | dt = np.dtype('i4') 69 | print (dt) 70 | 71 | 72 | # ### Example 3 73 | 74 | # In[12]: 75 | 76 | 77 | # using endian notation 78 | import numpy as np 79 | dt = np.dtype('>i4') 80 | print (dt) 81 | 82 | 83 | # ### Example 4 84 | 85 | # In[13]: 86 | 87 | 88 | # first create structured data type 89 | import numpy as np 90 | dt = np.dtype([('age',np.int8)]) 91 | print (dt) 92 | 93 | 94 | # ### Example 5 95 | 96 | # In[14]: 97 | 98 | 99 | # now apply it to ndarray object 100 | import numpy as np 101 | 102 | dt = np.dtype([('age',np.int8)]) 103 | a = np.array([(10,),(20,),(30,)], dtype = dt) 104 | print (a) 105 | 106 | 107 | # ### Example 6 108 | 109 | # In[15]: 110 | 111 | 112 | # file name can be used to access content of age column 113 | import numpy as np 114 | 115 | dt = np.dtype([('age',np.int8)]) 116 | a = np.array([(10,),(20,),(30,)], dtype = dt) 117 | print (a['age']) 118 | 119 | 120 | # ### Example 7 121 | 122 | # In[16]: 123 | 124 | 125 | import numpy as np 126 | student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) 127 | print (student) 128 | 129 | 130 | # ### Example 8 131 | 132 | # In[17]: 133 | 134 | 135 | import numpy as np 136 | 137 | student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) 138 | a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) 139 | print (a) 140 | 141 | 142 | # In[ ]: 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.10-Sort.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | import numpy as np 8 | a = np.array([[3,7],[9,1]]) 9 | 10 | print ('Our array is:') 11 | print (a) 12 | print ('\n') 13 | 14 | print ('Applying sort() function:') 15 | print (np.sort(a)) 16 | print ('\n') 17 | 18 | print ('Sort along axis 0:') 19 | print (np.sort(a, axis = 0)) 20 | print ('\n') 21 | 22 | # Order parameter in sort function 23 | dt = (np.dtype([('name', 'S10'),('age', int)])) 24 | a = (np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)) 25 | 26 | print ('Our array is:') 27 | print (a) 28 | print ('\n') 29 | 30 | print ('Order by name:') 31 | print (np.sort(a, order = 'name')) 32 | 33 | 34 | # ### numpy.argsort() 35 | # 36 | 37 | # In[4]: 38 | 39 | 40 | import numpy as np 41 | x = np.array([3, 1, 2]) 42 | 43 | print ('Our array is:') 44 | print (x) 45 | print ('\n') 46 | 47 | print ('Applying argsort() to x:') 48 | y = np.argsort(x) 49 | print (y) 50 | print ('\n') 51 | 52 | print ('Reconstruct original array in sorted order:') 53 | print (x[y]) 54 | print ('\n') 55 | 56 | print ('Reconstruct the original array using loop:') 57 | for i in y: 58 | print (x[i]) 59 | 60 | 61 | # ### numpy.lexsort() 62 | # 63 | 64 | # In[5]: 65 | 66 | 67 | import numpy as np 68 | 69 | nm = ('raju','anil','ravi','amar') 70 | dv = ('f.y.', 's.y.', 's.y.', 'f.y.') 71 | ind = np.lexsort((dv,nm)) 72 | 73 | print ('Applying lexsort() function:') 74 | print (ind) 75 | print ('\n') 76 | 77 | print ('Use this index to get sorted data:') 78 | print ([nm[i] + ", " + dv[i] for i in ind]) 79 | 80 | 81 | # ### numpy.argmax() and numpy.argmin() 82 | # 83 | 84 | # In[6]: 85 | 86 | 87 | import numpy as np 88 | a = np.array([[30,40,70],[80,20,10],[50,90,60]]) 89 | 90 | print ('Our array is:') 91 | print (a) 92 | print ('\n') 93 | 94 | print ('Applying argmax() function:') 95 | print (np.argmax(a)) 96 | print ('\n') 97 | 98 | print ('Index of maximum number in flattened array') 99 | print (a.flatten()) 100 | print ('\n') 101 | 102 | print ('Array containing indices of maximum along axis 0:') 103 | maxindex = np.argmax(a, axis = 0) 104 | print (maxindex) 105 | print ('\n') 106 | 107 | print ('Array containing indices of maximum along axis 1:') 108 | maxindex = np.argmax(a, axis = 1) 109 | print (maxindex) 110 | print ('\n') 111 | 112 | print ('Applying argmin() function:') 113 | minindex = np.argmin(a) 114 | print (minindex) 115 | print ('\n') 116 | 117 | print ('Flattened array:') 118 | print (a.flatten()[minindex]) 119 | print ('\n') 120 | 121 | print ('Flattened array along axis 0:') 122 | minindex = np.argmin(a, axis = 0) 123 | print (minindex) 124 | print ('\n') 125 | 126 | print ('Flattened array along axis 1:') 127 | minindex = np.argmin(a, axis = 1) 128 | print (minindex) 129 | 130 | 131 | # ### numpy.nonzero() 132 | # 133 | 134 | # In[7]: 135 | 136 | 137 | import numpy as np 138 | a = np.array([[30,40,0],[0,20,10],[50,0,60]]) 139 | 140 | print ('Our array is:') 141 | print (a) 142 | print ('\n') 143 | 144 | print ('Applying nonzero() function:') 145 | print (np.nonzero (a)) 146 | 147 | 148 | # ### numpy.where() 149 | # 150 | 151 | # In[8]: 152 | 153 | 154 | import numpy as np 155 | x = np.arange(9.).reshape(3, 3) 156 | 157 | print ('Our array is:') 158 | print (x) 159 | 160 | print ('Indices of elements > 3') 161 | y = np.where(x > 3) 162 | print (y) 163 | 164 | print ('Use these indices to get elements satisfying the condition') 165 | print (x[y]) 166 | 167 | 168 | # ### numpy.extract() 169 | # 170 | 171 | # In[10]: 172 | 173 | 174 | import numpy as np 175 | x = np.arange(9.).reshape(3, 3) 176 | 177 | print ('Our array is:') 178 | print (x) 179 | 180 | # define a condition 181 | condition = np.mod(x,2) == 0 182 | 183 | print ('Element-wise value of condition') 184 | print (condition) 185 | 186 | print ('Extract elements using condition') 187 | print (np.extract(condition, x)) 188 | 189 | 190 | # In[ ]: 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.11-Byte Swapping.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### numpy.ndarray.byteswap() 5 | # 6 | 7 | # In[6]: 8 | 9 | 10 | import numpy as np 11 | a = np.array([1, 256, 8755], dtype = np.int16) 12 | 13 | print ('Our array is:') 14 | print (a) 15 | 16 | print ('Representation of data in memory in hexadecimal form:') 17 | print (map(hex,a)) 18 | 19 | # byteswap() function swaps in place by passing True parameter 20 | 21 | print ('Applying byteswap() function:') 22 | print (a.byteswap(True)) 23 | 24 | print ('In hexadecimal form:') 25 | print (map(hex,a)) 26 | # We can see the bytes being swapped 27 | 28 | 29 | # ### No Copy 30 | # 31 | 32 | # In[9]: 33 | 34 | 35 | import numpy as np 36 | a = np.arange(6) 37 | 38 | print ('Our array is:') 39 | print (a) 40 | 41 | print ('Applying id() function:') 42 | print (id(a)) 43 | 44 | print ('a is assigned to b:') 45 | b = a 46 | print (b) 47 | 48 | print ('b has same id():') 49 | print (id(b)) 50 | 51 | print ('Change shape of b:') 52 | b.shape = 3,2 53 | print (b) 54 | 55 | print ('Shape of a also gets changed:') 56 | print (a) 57 | 58 | 59 | # ### View or Shallow Copy 60 | # 61 | 62 | # In[10]: 63 | 64 | 65 | import numpy as np 66 | # To begin with, a is 3X2 array 67 | a = np.arange(6).reshape(3,2) 68 | 69 | print ('Array a:') 70 | print (a) 71 | 72 | print ('Create view of a:') 73 | b = (a.view()) 74 | print (b) 75 | 76 | print ('id() for both the arrays are different:') 77 | print ('id() of a:') 78 | print (id(a)) 79 | print ('id() of b:') 80 | print (id(b)) 81 | 82 | # Change the shape of b. It does not change the shape of a 83 | b.shape = 2,3 84 | 85 | print ('Shape of b:') 86 | print (b) 87 | 88 | print ('Shape of a:') 89 | print (a) 90 | 91 | 92 | # In[11]: 93 | 94 | 95 | import numpy as np 96 | a = np.array([[10,10], [2,3], [4,5]]) 97 | 98 | print ('Our array is:') 99 | print (a) 100 | 101 | print ('Create a slice:') 102 | s = a[:, :2] 103 | print (s) 104 | 105 | 106 | # ### Deep Copy 107 | # 108 | 109 | # In[15]: 110 | 111 | 112 | import numpy as np 113 | a = np.array([[10,10], [2,3], [4,5]]) 114 | 115 | print ('Array a is:') 116 | print (a) 117 | 118 | print ('Create a deep copy of a:') 119 | b = a.copy() 120 | print ('Array b is:') 121 | print (b) 122 | 123 | #b does not share any memory of a 124 | print ('Can we write b is a') 125 | print (b is a) 126 | 127 | print ('Change the contents of b:') 128 | b[0,0] = 100 129 | 130 | print ('Modified array b:') 131 | print (b) 132 | 133 | print ('a remains unchanged:') 134 | print (a) 135 | 136 | 137 | # In[ ]: 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.12-Matrix Library.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### matlib.empty() 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import numpy.matlib 11 | import numpy as np 12 | 13 | print (np.matlib.empty((2,2))) 14 | # filled with random data 15 | 16 | 17 | # ### numpy.matlib.zeros() 18 | # 19 | 20 | # In[2]: 21 | 22 | 23 | import numpy.matlib 24 | import numpy as np 25 | print (np.matlib.zeros((2,2))) 26 | 27 | 28 | # ### numpy.matlib.ones() 29 | # 30 | 31 | # In[3]: 32 | 33 | 34 | import numpy.matlib 35 | import numpy as np 36 | print (np.matlib.ones((2,2))) 37 | 38 | 39 | # ### numpy.matlib.eye() 40 | # 41 | 42 | # In[4]: 43 | 44 | 45 | import numpy.matlib 46 | import numpy as np 47 | print (np.matlib.eye(n = 3, M = 4, k = 0, dtype = float)) 48 | 49 | 50 | # ### numpy.matlib.identity() 51 | # 52 | 53 | # In[5]: 54 | 55 | 56 | import numpy.matlib 57 | import numpy as np 58 | print (np.matlib.identity(5, dtype = float)) 59 | 60 | 61 | # ### numpy.matlib.rand() 62 | # 63 | 64 | # In[6]: 65 | 66 | 67 | import numpy.matlib 68 | import numpy as np 69 | print (np.matlib.rand(3,3)) 70 | 71 | 72 | # In[7]: 73 | 74 | 75 | import numpy.matlib 76 | import numpy as np 77 | 78 | i = np.matrix('1,2;3,4') 79 | print (i) 80 | 81 | 82 | # In[8]: 83 | 84 | 85 | import numpy.matlib 86 | import numpy as np 87 | 88 | j = np.asarray(i) 89 | print (j) 90 | 91 | 92 | # In[9]: 93 | 94 | 95 | import numpy.matlib 96 | import numpy as np 97 | 98 | k = np.asmatrix (j) 99 | print (k) 100 | 101 | 102 | # In[ ]: 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.13-NumPy Matplotlib.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Example 5 | 6 | # In[1]: 7 | 8 | 9 | import numpy as np 10 | from matplotlib import pyplot as plt 11 | 12 | x = np.arange(1,11) 13 | y = 2 * x + 5 14 | plt.title("Matplotlib demo") 15 | plt.xlabel("x axis caption") 16 | plt.ylabel("y axis caption") 17 | plt.plot(x,y) 18 | plt.show() 19 | 20 | 21 | # ### Example 22 | # 23 | 24 | # In[2]: 25 | 26 | 27 | import numpy as np 28 | from matplotlib import pyplot as plt 29 | 30 | x = np.arange(1,11) 31 | y = 2 * x + 5 32 | plt.title("Matplotlib demo") 33 | plt.xlabel("x axis caption") 34 | plt.ylabel("y axis caption") 35 | plt.plot(x,y,"ob") 36 | plt.show() 37 | 38 | 39 | # ### Sine Wave Plot 40 | # 41 | 42 | # In[3]: 43 | 44 | 45 | import numpy as np 46 | import matplotlib.pyplot as plt 47 | 48 | # Compute the x and y coordinates for points on a sine curve 49 | x = np.arange(0, 3 * np.pi, 0.1) 50 | y = np.sin(x) 51 | plt.title("sine wave form") 52 | 53 | # Plot the points using matplotlib 54 | plt.plot(x, y) 55 | plt.show() 56 | 57 | 58 | # ### subplot() 59 | # 60 | 61 | # In[4]: 62 | 63 | 64 | import numpy as np 65 | import matplotlib.pyplot as plt 66 | 67 | # Compute the x and y coordinates for points on sine and cosine curves 68 | x = np.arange(0, 3 * np.pi, 0.1) 69 | y_sin = np.sin(x) 70 | y_cos = np.cos(x) 71 | 72 | # Set up a subplot grid that has height 2 and width 1, 73 | # and set the first such subplot as active. 74 | plt.subplot(2, 1, 1) 75 | 76 | # Make the first plot 77 | plt.plot(x, y_sin) 78 | plt.title('Sine') 79 | 80 | # Set the second subplot as active, and make the second plot. 81 | plt.subplot(2, 1, 2) 82 | plt.plot(x, y_cos) 83 | plt.title('Cosine') 84 | 85 | # Show the figure. 86 | plt.show() 87 | 88 | 89 | # ### bar() 90 | # 91 | 92 | # In[5]: 93 | 94 | 95 | from matplotlib import pyplot as plt 96 | x = [5,8,10] 97 | y = [12,16,6] 98 | 99 | x2 = [6,9,11] 100 | y2 = [6,15,7] 101 | plt.bar(x, y, align = 'center') 102 | plt.bar(x2, y2, color = 'g', align = 'center') 103 | plt.title('Bar graph') 104 | plt.ylabel('Y axis') 105 | plt.xlabel('X axis') 106 | 107 | plt.show() 108 | 109 | 110 | # In[ ]: 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.14-Histogram Using Matplotlib.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### numpy.histogram() 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import numpy as np 11 | 12 | a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) 13 | np.histogram(a,bins = [0,20,40,60,80,100]) 14 | hist,bins = np.histogram(a,bins = [0,20,40,60,80,100]) 15 | print (hist) 16 | print (bins) 17 | 18 | 19 | # ### plt() 20 | # 21 | 22 | # In[2]: 23 | 24 | 25 | from matplotlib import pyplot as plt 26 | import numpy as np 27 | 28 | a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) 29 | plt.hist(a, bins = [0,20,40,60,80,100]) 30 | plt.title("histogram") 31 | plt.show() 32 | 33 | 34 | # ### numpy.save() 35 | # 36 | 37 | # In[3]: 38 | 39 | 40 | import numpy as np 41 | a = np.array([1,2,3,4,5]) 42 | np.save('outfile',a) 43 | 44 | 45 | # In[4]: 46 | 47 | 48 | import numpy as np 49 | b = np.load('outfile.npy') 50 | print (b) 51 | 52 | 53 | # ### savetxt() 54 | # 55 | 56 | # In[5]: 57 | 58 | 59 | import numpy as np 60 | 61 | a = np.array([1,2,3,4,5]) 62 | np.savetxt('out.txt',a) 63 | b = np.loadtxt('out.txt') 64 | print (b) 65 | 66 | 67 | # In[ ]: 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.2-Array Attributes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## ndarray.shape 5 | # 6 | 7 | # ### Example 1 8 | # 9 | 10 | # In[1]: 11 | 12 | 13 | import numpy as np 14 | a = np.array([[1,2,3],[4,5,6]]) 15 | print (a.shape) 16 | 17 | 18 | # ### Example 2 19 | # 20 | 21 | # In[3]: 22 | 23 | 24 | # this resizes the ndarray 25 | import numpy as np 26 | 27 | a = np.array([[1,2,3],[4,5,6]]) 28 | a.shape = (3,2) 29 | print (a) 30 | 31 | 32 | # ### Example 3 33 | # 34 | 35 | # In[4]: 36 | 37 | 38 | import numpy as np 39 | a = np.array([[1,2,3],[4,5,6]]) 40 | b = a.reshape(3,2) 41 | print (b) 42 | 43 | 44 | # ## ndarray.ndim 45 | # 46 | 47 | # ### Example 1 48 | # 49 | 50 | # In[5]: 51 | 52 | 53 | # an array of evenly spaced numbers 54 | import numpy as np 55 | a = np.arange(24) 56 | print (a) 57 | 58 | 59 | # ### Example 2 60 | # 61 | 62 | # In[6]: 63 | 64 | 65 | # this is one dimensional array 66 | import numpy as np 67 | a = np.arange(24) 68 | a.ndim 69 | 70 | # now reshape it 71 | b = a.reshape(2,4,3) 72 | print (b) 73 | # b is having three dimensions 74 | 75 | 76 | # ## numpy.itemsize 77 | # 78 | 79 | # ### Example 1 80 | # 81 | 82 | # In[7]: 83 | 84 | 85 | # dtype of array is int8 (1 byte) 86 | import numpy as np 87 | x = np.array([1,2,3,4,5], dtype = np.int8) 88 | print (x.itemsize) 89 | 90 | 91 | # ### Example 2 92 | # 93 | 94 | # In[8]: 95 | 96 | 97 | # dtype of array is now float32 (4 bytes) 98 | import numpy as np 99 | x = np.array([1,2,3,4,5], dtype = np.float32) 100 | print (x.itemsize) 101 | 102 | 103 | # ### numpy.flags 104 | # 105 | 106 | # In[9]: 107 | 108 | 109 | import numpy as np 110 | x = np.array([1,2,3,4,5]) 111 | print (x.flags) 112 | 113 | 114 | # ### numpy.empty 115 | # 116 | 117 | # In[17]: 118 | 119 | 120 | import numpy as np 121 | x = np.empty([3,2], dtype = int) 122 | print (x) 123 | 124 | 125 | # ### numpy.zeros 126 | # 127 | 128 | # ### Example 1 129 | # 130 | 131 | # In[11]: 132 | 133 | 134 | # array of five zeros. Default dtype is float 135 | import numpy as np 136 | x = np.zeros(5) 137 | print (x) 138 | 139 | 140 | # ### Example 2 141 | # 142 | 143 | # In[12]: 144 | 145 | 146 | import numpy as np 147 | x = np.zeros((5,), dtype = np.int) 148 | print (x) 149 | 150 | 151 | # ### Example 3 152 | # 153 | 154 | # In[13]: 155 | 156 | 157 | # custom type 158 | import numpy as np 159 | x = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')]) 160 | print (x) 161 | 162 | 163 | # ### numpy.ones 164 | # 165 | 166 | # ### Example 1 167 | # 168 | 169 | # In[14]: 170 | 171 | 172 | # array of five ones. Default dtype is float 173 | import numpy as np 174 | x = np.ones(5) 175 | print (x) 176 | 177 | 178 | # ### Example 2 179 | # 180 | 181 | # In[15]: 182 | 183 | 184 | import numpy as np 185 | x = np.ones([2,2], dtype = int) 186 | print (x) 187 | 188 | 189 | # In[ ]: 190 | 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.3-Array From Existing Data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## numpy.asarray 5 | # 6 | 7 | # ### Example 1 8 | # 9 | 10 | # In[1]: 11 | 12 | 13 | # convert list to ndarray 14 | import numpy as np 15 | 16 | x = [1,2,3] 17 | a = np.asarray(x) 18 | print (a) 19 | 20 | 21 | # ### Example 2 22 | # 23 | 24 | # In[19]: 25 | 26 | 27 | # dtype is set 28 | import numpy as np 29 | 30 | x = [1,2,3] 31 | a = np.asarray(x, dtype = float) 32 | print (a) 33 | 34 | 35 | # ### Example 3 36 | # 37 | 38 | # In[3]: 39 | 40 | 41 | # ndarray from tuple 42 | import numpy as np 43 | 44 | x = (1,2,3) 45 | a = np.asarray(x) 46 | print (a) 47 | 48 | 49 | # ### Example 4 50 | # 51 | 52 | # In[4]: 53 | 54 | 55 | # ndarray from list of tuples 56 | import numpy as np 57 | 58 | x = [(1,2,3),(4,5)] 59 | a = np.asarray(x) 60 | print (a) 61 | 62 | 63 | # ### numpy.fromiter 64 | 65 | # In[10]: 66 | 67 | 68 | # obtain iterator object from list 69 | import numpy as np 70 | list = range(5) 71 | it = iter(list) 72 | 73 | # use iterator to create ndarray 74 | x = np.fromiter(it, dtype = float) 75 | print (x) 76 | 77 | 78 | # ### numpy.arange 79 | # 80 | 81 | # ### Example 1 82 | # 83 | 84 | # In[11]: 85 | 86 | 87 | import numpy as np 88 | x = np.arange(5) 89 | print (x) 90 | 91 | 92 | # ### Example 2 93 | # 94 | 95 | # In[12]: 96 | 97 | 98 | import numpy as np 99 | # dtype set 100 | x = np.arange(5, dtype = float) 101 | print (x) 102 | 103 | 104 | # ### Example 3 105 | # 106 | 107 | # In[13]: 108 | 109 | 110 | # start and stop parameters set 111 | import numpy as np 112 | x = np.arange(10,20,2) 113 | print (x) 114 | 115 | 116 | # ### numpy.linspace 117 | # 118 | 119 | # ### Example 1 120 | # 121 | 122 | # In[14]: 123 | 124 | 125 | import numpy as np 126 | x = np.linspace(10,20,5) 127 | print (x) 128 | 129 | 130 | # ### Example 2 131 | # 132 | 133 | # In[15]: 134 | 135 | 136 | # endpoint set to false 137 | import numpy as np 138 | x = np.linspace(10,20, 5, endpoint = False) 139 | print (x) 140 | 141 | 142 | # ### Example 3 143 | # 144 | 145 | # In[16]: 146 | 147 | 148 | # find retstep value 149 | import numpy as np 150 | 151 | x = np.linspace(1,2,5, retstep = True) 152 | print (x) 153 | # retstep here is 0.25 154 | 155 | 156 | # ### numpy.logspace 157 | # 158 | 159 | # ### Example 1 160 | # 161 | 162 | # In[17]: 163 | 164 | 165 | import numpy as np 166 | # default base is 10 167 | a = np.logspace(1.0, 2.0, num = 10) 168 | print (a) 169 | 170 | 171 | # ### Example 2 172 | # 173 | 174 | # In[18]: 175 | 176 | 177 | # set base of log space to 2 178 | import numpy as np 179 | a = np.logspace(1,10,num = 10, base = 2) 180 | print (a) 181 | 182 | 183 | # In[ ]: 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.4-Indexing & Slicing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Example 1 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import numpy as np 11 | a = np.arange(10) 12 | s = slice(2,7,2) 13 | print (a[s]) 14 | 15 | 16 | # ### Example 2 17 | # 18 | 19 | # In[2]: 20 | 21 | 22 | import numpy as np 23 | a = np.arange(10) 24 | b = a[2:7:2] 25 | print (b) 26 | 27 | 28 | # ### Example 3 29 | # 30 | 31 | # In[3]: 32 | 33 | 34 | # slice single item 35 | import numpy as np 36 | 37 | a = np.arange(10) 38 | b = a[5] 39 | print (b) 40 | 41 | 42 | # ### Example 4 43 | # 44 | 45 | # In[4]: 46 | 47 | 48 | # slice items starting from index 49 | import numpy as np 50 | a = np.arange(10) 51 | print (a[2:]) 52 | 53 | 54 | # ### Example 5 55 | # 56 | 57 | # In[5]: 58 | 59 | 60 | # slice items between indexes 61 | import numpy as np 62 | a = np.arange(10) 63 | print (a[2:5]) 64 | 65 | 66 | # ### Example 6 67 | # 68 | 69 | # In[7]: 70 | 71 | 72 | import numpy as np 73 | a = np.array([[1,2,3],[3,4,5],[4,5,6]]) 74 | print (a) 75 | 76 | # slice items starting from index 77 | print ('Now we will slice the array from the index a[1:]') 78 | print (a[1:]) 79 | 80 | 81 | # ### Example 7 82 | # 83 | 84 | # In[8]: 85 | 86 | 87 | # array to begin with 88 | import numpy as np 89 | a = np.array([[1,2,3],[3,4,5],[4,5,6]]) 90 | 91 | print ('Our array is:') 92 | print (a) 93 | print ('\n') 94 | 95 | # this returns array of items in the second column 96 | print ('The items in the second column are:') 97 | print (a[...,1]) 98 | print ('\n') 99 | 100 | # Now we will slice all items from the second row 101 | print ('The items in the second row are:') 102 | print (a[1,...]) 103 | print ('\n') 104 | 105 | # Now we will slice all items from column 1 onwards 106 | print ('The items column 1 onwards are:') 107 | print (a[...,1:]) 108 | 109 | 110 | # ### Integer Indexing 111 | # 112 | 113 | # ### Example 1 114 | # 115 | 116 | # In[9]: 117 | 118 | 119 | import numpy as np 120 | 121 | x = np.array([[1, 2], [3, 4], [5, 6]]) 122 | y = x[[0,1,2], [0,1,0]] 123 | print (y) 124 | 125 | 126 | # ### Example 2 127 | # 128 | 129 | # In[11]: 130 | 131 | 132 | import numpy as np 133 | x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) 134 | 135 | print ('Our array is:') 136 | print (x) 137 | print ('\n') 138 | 139 | rows = np.array([[0,0],[3,3]]) 140 | cols = np.array([[0,2],[0,2]]) 141 | y = x[rows,cols] 142 | 143 | print ('The corner elements of this array are:') 144 | print (y) 145 | 146 | 147 | # ### Example 3 148 | # 149 | 150 | # In[12]: 151 | 152 | 153 | import numpy as np 154 | x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) 155 | 156 | print ('Our array is:') 157 | print (x) 158 | print ('\n') 159 | 160 | # slicing 161 | z = x[1:4,1:3] 162 | 163 | print ('After slicing, our array becomes:') 164 | print (z) 165 | print ('\n') 166 | 167 | # using advanced index for column 168 | y = x[1:4,[1,2]] 169 | 170 | print ('Slicing using advanced index for column:') 171 | print (y) 172 | 173 | 174 | # ### Boolean Array Indexing 175 | # 176 | 177 | # ### Example 1 178 | # 179 | 180 | # In[13]: 181 | 182 | 183 | import numpy as np 184 | x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) 185 | 186 | print ('Our array is:') 187 | print (x) 188 | print ('\n') 189 | 190 | # Now we will print the items greater than 5 191 | print ('The items greater than 5 are:') 192 | print (x[x > 5]) 193 | 194 | 195 | # ### Example 2 196 | # 197 | 198 | # In[15]: 199 | 200 | 201 | import numpy as np 202 | a = np.array([np.nan, 1,2,np.nan,3,4,5]) 203 | print (a[~np.isnan(a)]) 204 | 205 | 206 | # ### Example 3 207 | # 208 | 209 | # In[16]: 210 | 211 | 212 | import numpy as np 213 | a = np.array([1, 2+6j, 5, 3.5+5j]) 214 | print (a[np.iscomplex(a)]) 215 | 216 | 217 | # In[ ]: 218 | 219 | 220 | 221 | 222 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.5-Broadcasting.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## Broadcasting 5 | 6 | # ### Example 1 7 | 8 | # In[2]: 9 | 10 | 11 | import numpy as np 12 | a = np.array([1,2,3,4]) 13 | b = np.array([10,20,30,40]) 14 | c = a * b 15 | print (c) 16 | 17 | 18 | # ### Example 2 19 | 20 | # In[4]: 21 | 22 | 23 | import numpy as np 24 | a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]]) 25 | b = np.array([1.0,2.0,3.0]) 26 | 27 | print ('First array:') 28 | print (a) 29 | print ('\n') 30 | 31 | print ('Second array:') 32 | print (b) 33 | print ('\n') 34 | 35 | print ('First Array + Second Array') 36 | print (a + b) 37 | 38 | 39 | # ## Iterating Over Array 40 | 41 | # ### Example 1 42 | # 43 | 44 | # In[7]: 45 | 46 | 47 | import numpy as np 48 | a = np.arange(0,60,5) 49 | a = a.reshape(3,4) 50 | 51 | print ('Original array is:') 52 | print (a) 53 | print ('\n') 54 | 55 | print ('Modified array is:') 56 | for x in np.nditer(a): 57 | print (x) 58 | 59 | 60 | # ### Example 2 61 | # 62 | 63 | # In[9]: 64 | 65 | 66 | import numpy as np 67 | a = np.arange(0,60,5) 68 | a = a.reshape(3,4) 69 | 70 | print ('Original array is:') 71 | print (a) 72 | print ('\n') 73 | 74 | print ('Transpose of the original array is:') 75 | b = a.T 76 | print (b) 77 | print ('\n') 78 | 79 | print ('Modified array is:') 80 | for x in np.nditer(b): 81 | print (x) 82 | 83 | 84 | # ## Iteration Order 85 | # 86 | 87 | # ### Example 1 88 | # 89 | 90 | # In[11]: 91 | 92 | 93 | import numpy as np 94 | a = np.arange(0,60,5) 95 | a = a.reshape(3,4) 96 | print ('Original array is:') 97 | print (a) 98 | print ('\n') 99 | 100 | print ('Transpose of the original array is:') 101 | b = a.T 102 | print (b) 103 | print ('\n') 104 | 105 | print ('Sorted in C-style order:') 106 | c = b.copy(order = 'C') 107 | print (c) 108 | for x in np.nditer(c): 109 | print (x) 110 | 111 | print ('\n') 112 | 113 | print ('Sorted in F-style order:') 114 | c = b.copy(order = 'F') 115 | print (c) 116 | for x in np.nditer(c): 117 | print (x) 118 | 119 | 120 | # ### Example 2 121 | # 122 | 123 | # In[12]: 124 | 125 | 126 | import numpy as np 127 | a = np.arange(0,60,5) 128 | a = a.reshape(3,4) 129 | 130 | print ('Original array is:') 131 | print (a) 132 | print ('\n') 133 | 134 | print ('Sorted in C-style order:') 135 | for x in np.nditer(a, order = 'C'): 136 | print (x) 137 | print ('\n') 138 | 139 | print ('Sorted in F-style order:') 140 | for x in np.nditer(a, order = 'F'): 141 | print (x) 142 | 143 | 144 | # ### Modifying Array Values 145 | # 146 | 147 | # In[13]: 148 | 149 | 150 | import numpy as np 151 | a = np.arange(0,60,5) 152 | a = a.reshape(3,4) 153 | print ('Original array is:') 154 | print (a) 155 | print ('\n') 156 | 157 | for x in np.nditer(a, op_flags = ['readwrite']): 158 | x[...] = 2*x 159 | print ('Modified array is:') 160 | print (a) 161 | 162 | 163 | # ### External Loop 164 | # 165 | 166 | # In[14]: 167 | 168 | 169 | import numpy as np 170 | a = np.arange(0,60,5) 171 | a = a.reshape(3,4) 172 | 173 | print ('Original array is:') 174 | print (a) 175 | print ('\n') 176 | 177 | print ('Modified array is:') 178 | for x in np.nditer(a, flags = ['external_loop'], order = 'F'): 179 | print (x) 180 | 181 | 182 | # ### Broadcasting Iteration 183 | # 184 | 185 | # In[15]: 186 | 187 | 188 | import numpy as np 189 | a = np.arange(0,60,5) 190 | a = a.reshape(3,4) 191 | 192 | print ('First array is:') 193 | print (a) 194 | print ('\n') 195 | 196 | print ('Second array is:') 197 | b = np.array([1, 2, 3, 4], dtype = int) 198 | print (b) 199 | print ('\n') 200 | 201 | print ('Modified array is:') 202 | for x,y in np.nditer([a,b]): 203 | print ("%d:%d" % (x,y)) 204 | 205 | 206 | # In[ ]: 207 | 208 | 209 | 210 | 211 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.7-Mathematical Functions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## Trigonometric Functions 5 | # 6 | 7 | # ### Example 1 8 | 9 | # In[1]: 10 | 11 | 12 | import numpy as np 13 | a = np.array([0,30,45,60,90]) 14 | 15 | print ('Sine of different angles:') 16 | # Convert to radians by multiplying with pi/180 17 | print (np.sin(a*np.pi/180)) 18 | print ('\n') 19 | 20 | print ('Cosine values for angles in array:') 21 | print (np.cos(a*np.pi/180)) 22 | print ('\n') 23 | 24 | print ('Tangent values for given angles:') 25 | print (np.tan(a*np.pi/180)) 26 | 27 | 28 | # ### Example 2 29 | 30 | # In[3]: 31 | 32 | 33 | import numpy as np 34 | a = np.array([0,30,45,60,90]) 35 | 36 | print ('Array containing sine values:') 37 | sin = np.sin(a*np.pi/180) 38 | print (sin) 39 | print ('\n') 40 | 41 | print ('Compute sine inverse of angles. Returned values are in radians.') 42 | inv = np.arcsin(sin) 43 | print (inv) 44 | print ('\n') 45 | 46 | print ('Check result by converting to degrees:') 47 | print (np.degrees(inv)) 48 | print ('\n') 49 | 50 | print ('arccos and arctan functions behave similarly:') 51 | cos = np.cos(a*np.pi/180) 52 | print (cos) 53 | print ('\n') 54 | 55 | print ('Inverse of cos:') 56 | inv = np.arccos(cos) 57 | print (inv) 58 | print ('\n') 59 | 60 | print ('In degrees:') 61 | print (np.degrees(inv)) 62 | print ('\n') 63 | 64 | print ('Tan function:') 65 | tan = np.tan(a*np.pi/180) 66 | print (tan) 67 | print ('\n') 68 | 69 | print ('Inverse of tan:') 70 | inv = np.arctan(tan) 71 | print (inv) 72 | print ('\n') 73 | 74 | print ('In degrees:') 75 | print (np.degrees(inv)) 76 | 77 | 78 | # ### Functions for Rounding 79 | # 80 | 81 | # In[4]: 82 | 83 | 84 | import numpy as np 85 | a = np.array([1.0,5.55, 123, 0.567, 25.532]) 86 | 87 | print ('Original array:') 88 | print (a) 89 | print ('\n') 90 | 91 | print ('After rounding:') 92 | print (np.around(a)) 93 | print (np.around(a, decimals = 1)) 94 | print (np.around(a, decimals = -1)) 95 | 96 | 97 | # ### numpy.floor() 98 | # 99 | 100 | # In[5]: 101 | 102 | 103 | import numpy as np 104 | a = np.array([-1.7, 1.5, -0.2, 0.6, 10]) 105 | 106 | print ('The given array:') 107 | print (a) 108 | print ('\n') 109 | 110 | print ('The modified array:') 111 | print (np.floor(a)) 112 | 113 | 114 | # ### numpy.ceil() 115 | # 116 | 117 | # In[6]: 118 | 119 | 120 | import numpy as np 121 | a = np.array([-1.7, 1.5, -0.2, 0.6, 10]) 122 | 123 | print ('The given array:') 124 | print (a) 125 | print ('\n') 126 | 127 | print ('The modified array:') 128 | print (np.ceil(a)) 129 | 130 | 131 | # In[ ]: 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.8-Arithmetic Operations.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | import numpy as np 8 | a = np.arange(9, dtype = np.float_).reshape(3,3) 9 | 10 | print ('First array:') 11 | print (a) 12 | print ('\n') 13 | 14 | print ('Second array:') 15 | b = np.array([10,10,10]) 16 | print (b) 17 | print ('\n') 18 | 19 | print ('Add the two arrays:') 20 | print (np.add(a,b)) 21 | print ('\n') 22 | 23 | print ('Subtract the two arrays:') 24 | print (np.subtract(a,b)) 25 | print ('\n') 26 | 27 | print ('Multiply the two arrays:') 28 | print (np.multiply(a,b)) 29 | print ('\n') 30 | 31 | print ('Divide the two arrays:') 32 | print (np.divide(a,b)) 33 | 34 | 35 | # ### numpy.reciprocal() 36 | # 37 | 38 | # In[2]: 39 | 40 | 41 | import numpy as np 42 | a = np.array([0.25, 1.33, 1, 0, 100]) 43 | 44 | print ('Our array is:') 45 | print (a) 46 | print ('\)n') 47 | 48 | print ('After applying reciprocal function:') 49 | print (np.reciprocal(a)) 50 | print ('\n') 51 | 52 | b = np.array([100], dtype = int) 53 | print ('The second array is:') 54 | print (b) 55 | print ('\n') 56 | 57 | print ('After applying reciprocal function:') 58 | print (np.reciprocal(b)) 59 | 60 | 61 | # ### numpy.power() 62 | # 63 | 64 | # In[3]: 65 | 66 | 67 | import numpy as np 68 | a = np.array([10,100,1000]) 69 | 70 | print ('Our array is:') 71 | print (a) 72 | print ('\n') 73 | 74 | print ('Applying power function:') 75 | print (np.power(a,2)) 76 | print ('\n') 77 | 78 | print ('Second array:') 79 | b = np.array([1,2,3]) 80 | print (b) 81 | print ('\n') 82 | 83 | print ('Applying power function again:') 84 | print (np.power(a,b)) 85 | 86 | 87 | # ### numpy.mod() 88 | # 89 | 90 | # In[4]: 91 | 92 | 93 | import numpy as np 94 | a = np.array([10,20,30]) 95 | b = np.array([3,5,7]) 96 | 97 | print ('First array:') 98 | print (a) 99 | print ('\n') 100 | 101 | print ('Second array:') 102 | print (b) 103 | print ('\n') 104 | 105 | print ('Applying mod() function:') 106 | print (np.mod(a,b)) 107 | print ('\n') 108 | 109 | print ('Applying remainder() function:') 110 | print (np.remainder(a,b)) 111 | 112 | 113 | # In[5]: 114 | 115 | 116 | import numpy as np 117 | a = np.array([-5.6j, 0.2j, 11. , 1+1j]) 118 | 119 | print ('Our array is:') 120 | print (a) 121 | print ('\n') 122 | 123 | print ('Applying real() function:') 124 | print (np.real(a)) 125 | print ('\n') 126 | 127 | print ('Applying imag() function:') 128 | print (np.imag(a)) 129 | print ('\n') 130 | 131 | print ('Applying conj() function:') 132 | print (np.conj(a)) 133 | print ('\n') 134 | 135 | print ('Applying angle() function:') 136 | print (np.angle(a)) 137 | print ('\n') 138 | 139 | print ('Applying angle() function again (result in degrees)') 140 | print (np.angle(a, deg = True)) 141 | 142 | 143 | # In[ ]: 144 | 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Code_Scripts/2.9-Statistical Functions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### numpy.amin() and numpy.amax() 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import numpy as np 11 | a = np.array([[3,7,5],[8,4,3],[2,4,9]]) 12 | 13 | print ('Our array is:') 14 | print (a) 15 | print ('\n') 16 | 17 | print ('Applying amin() function:') 18 | print (np.amin(a,1)) 19 | print ('\n') 20 | 21 | print ('Applying amin() function again:') 22 | print (np.amin(a,0)) 23 | print ('\n') 24 | 25 | print ('Applying amax() function:') 26 | print (np.amax(a)) 27 | print ('\n') 28 | 29 | print ('Applying amax() function again:') 30 | print (np.amax(a, axis = 0)) 31 | 32 | 33 | # ### numpy.ptp() 34 | # 35 | 36 | # In[2]: 37 | 38 | 39 | import numpy as np 40 | a = np.array([[3,7,5],[8,4,3],[2,4,9]]) 41 | 42 | print ('Our array is:') 43 | print (a) 44 | print ('\n') 45 | 46 | print ('Applying ptp() function:') 47 | print (np.ptp(a)) 48 | print ('\n') 49 | 50 | print ('Applying ptp() function along axis 1:') 51 | print (np.ptp(a, axis = 1)) 52 | print ('\n') 53 | 54 | print ('Applying ptp() function along axis 0:') 55 | print (np.ptp(a, axis = 0)) 56 | 57 | 58 | # ### numpy.percentile() 59 | # 60 | 61 | # In[3]: 62 | 63 | 64 | import numpy as np 65 | a = np.array([[30,40,70],[80,20,10],[50,90,60]]) 66 | 67 | print ('Our array is:') 68 | print (a) 69 | print ('\n') 70 | 71 | print ('Applying percentile() function:') 72 | print (np.percentile(a,50)) 73 | print ('\n') 74 | 75 | print ('Applying percentile() function along axis 1:') 76 | print (np.percentile(a,50, axis = 1)) 77 | print ('\n') 78 | 79 | print ('Applying percentile() function along axis 0:') 80 | print (np.percentile(a,50, axis = 0)) 81 | 82 | 83 | # ### numpy.median() 84 | # 85 | 86 | # In[4]: 87 | 88 | 89 | import numpy as np 90 | a = np.array([[30,65,70],[80,95,10],[50,90,60]]) 91 | 92 | print ('Our array is:') 93 | print (a) 94 | print ('\n') 95 | 96 | print ('Applying median() function:') 97 | print (np.median(a)) 98 | print ('\n') 99 | 100 | print ('Applying median() function along axis 0:') 101 | print (np.median(a, axis = 0)) 102 | print ('\n') 103 | 104 | print ('Applying median() function along axis 1:') 105 | print (np.median(a, axis = 1)) 106 | 107 | 108 | # ### numpy.mean() 109 | # 110 | 111 | # In[5]: 112 | 113 | 114 | import numpy as np 115 | a = np.array([[1,2,3],[3,4,5],[4,5,6]]) 116 | 117 | print ('Our array is:') 118 | print (a) 119 | print ('\n') 120 | 121 | print ('Applying mean() function:') 122 | print (np.mean(a)) 123 | print ('\n') 124 | 125 | print ('Applying mean() function along axis 0:') 126 | print (np.mean(a, axis = 0)) 127 | print ('\n') 128 | 129 | print ('Applying mean() function along axis 1:') 130 | print (np.mean(a, axis = 1)) 131 | 132 | 133 | # ### numpy.average() 134 | # 135 | 136 | # In[6]: 137 | 138 | 139 | import numpy as np 140 | a = np.array([1,2,3,4]) 141 | 142 | print ('Our array is:') 143 | print (a) 144 | print ('\n') 145 | 146 | print ('Applying average() function:') 147 | print (np.average(a)) 148 | print ('\n') 149 | 150 | # this is same as mean when weight is not specified 151 | wts = np.array([4,3,2,1]) 152 | 153 | print ('Applying average() function again:') 154 | print (np.average(a,weights = wts)) 155 | print ('\n') 156 | 157 | # Returns the sum of weights, if the returned parameter is set to True. 158 | print ('Sum of weights') 159 | print (np.average([1,2,3, 4],weights = [4,3,2,1], returned = True)) 160 | 161 | 162 | # In[7]: 163 | 164 | 165 | import numpy as np 166 | a = np.arange(6).reshape(3,2) 167 | 168 | print ('Our array is:') 169 | print (a) 170 | print ('\n') 171 | 172 | print ('Modified array:') 173 | wt = np.array([3,5]) 174 | print (np.average(a, axis = 1, weights = wt)) 175 | print ('\n') 176 | 177 | print ('Modified array:') 178 | print (np.average(a, axis = 1, weights = wt, returned = True)) 179 | 180 | 181 | # ### Standard Deviation 182 | # 183 | 184 | # In[8]: 185 | 186 | 187 | import numpy as np 188 | print (np.std([1,2,3,4])) 189 | 190 | 191 | # ### Variance 192 | # 193 | 194 | # In[9]: 195 | 196 | 197 | import numpy as np 198 | print (np.var([1,2,3,4])) 199 | 200 | 201 | # In[ ]: 202 | 203 | 204 | 205 | 206 | -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.1-Ndarray object/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.1-Ndarray object/1.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/1.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/2.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/3.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/4.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.13-Numpy matplotlib/5.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.14-Numpy histogram/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.14-Numpy histogram/1.jpg -------------------------------------------------------------------------------- /Volume_I/2-Numpy/Images/2.5-Numpy Broadcasting/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_I/2-Numpy/Images/2.5-Numpy Broadcasting/1.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.1-Data Structures.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # Pandas - Series 5 | 6 | # ### Create an Empty Series 7 | 8 | # In[ ]: 9 | 10 | 11 | #import the pandas library and aliasing as pd 12 | import pandas as pd 13 | s = pd.Series() 14 | print (s) 15 | 16 | 17 | # ### Create a Series from ndarray 18 | 19 | # #### Example 1 20 | 21 | # In[5]: 22 | 23 | 24 | #import the pandas library and aliasing as pd 25 | import pandas as pd 26 | import numpy as np 27 | data = np.array(['a','b','c','d']) 28 | s = pd.Series(data) 29 | print (s) 30 | 31 | 32 | # #### Example 2 33 | 34 | # In[6]: 35 | 36 | 37 | #import the pandas library and aliasing as pd 38 | import pandas as pd 39 | import numpy as np 40 | data = np.array(['a','b','c','d']) 41 | s = pd.Series(data,index=[100,101,102,103]) 42 | print (s) 43 | 44 | 45 | # ### Create a Series from dict 46 | 47 | # #### Example 1 48 | 49 | # In[ ]: 50 | 51 | 52 | #import the pandas library and aliasing as pd 53 | import pandas as pd 54 | import numpy as np 55 | data = {'a' : 0., 'b' : 1., 'c' : 2.} 56 | s = pd.Series(data) 57 | print (s) 58 | 59 | 60 | # #### Example 2 61 | 62 | # In[8]: 63 | 64 | 65 | #import the pandas library and aliasing as pd 66 | import pandas as pd 67 | import numpy as np 68 | data = {'a' : 0., 'b' : 1., 'c' : 2.} 69 | s = pd.Series(data,index=['b','c','d','a']) 70 | print (s) 71 | 72 | 73 | # ### Create a Series from Scalar 74 | 75 | # In[9]: 76 | 77 | 78 | #import the pandas library and aliasing as pd 79 | import pandas as pd 80 | import numpy as np 81 | s = pd.Series(5, index=[0, 1, 2, 3]) 82 | print (s) 83 | 84 | 85 | # ### Accessing Data from Series with Position 86 | 87 | # #### Example 1 88 | 89 | # In[10]: 90 | 91 | 92 | import pandas as pd 93 | s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) 94 | 95 | #retrieve the first element 96 | print (s[0]) 97 | 98 | 99 | # #### Example 2 100 | 101 | # In[11]: 102 | 103 | 104 | import pandas as pd 105 | s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) 106 | 107 | #retrieve the first three element 108 | print (s[:3]) 109 | 110 | 111 | # #### Example 3 112 | 113 | # In[12]: 114 | 115 | 116 | import pandas as pd 117 | s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) 118 | 119 | #retrieve the last three element 120 | print (s[-3:]) 121 | 122 | 123 | # ### Retrieve Data Using Label (Index) 124 | 125 | # #### Example 1 126 | 127 | # In[13]: 128 | 129 | 130 | import pandas as pd 131 | s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) 132 | 133 | #retrieve a single element 134 | print (s['a']) 135 | 136 | 137 | # #### Example 2 138 | 139 | # In[14]: 140 | 141 | 142 | import pandas as pd 143 | s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) 144 | 145 | #retrieve multiple elements 146 | print (s[['a','c','d']]) 147 | 148 | 149 | # #### Example 3 150 | 151 | # In[15]: 152 | 153 | 154 | import pandas as pd 155 | s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) 156 | 157 | #retrieve multiple elements 158 | print (s['f']) 159 | 160 | 161 | # In[ ]: 162 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.10-Indexing and Selecting Data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### .loc() 5 | # 6 | 7 | # ### Example 1 8 | # 9 | 10 | # In[1]: 11 | 12 | 13 | #import the pandas library and aliasing as pd 14 | import pandas as pd 15 | import numpy as np 16 | 17 | df = pd.DataFrame(np.random.randn(8, 4), 18 | index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) 19 | 20 | #select all rows for a specific column 21 | print (df.loc[:,'A']) 22 | 23 | 24 | # ### Example 2 25 | # 26 | 27 | # In[2]: 28 | 29 | 30 | # import the pandas library and aliasing as pd 31 | import pandas as pd 32 | import numpy as np 33 | 34 | df = pd.DataFrame(np.random.randn(8, 4), 35 | index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) 36 | 37 | # Select all rows for multiple columns, say list[] 38 | print (df.loc[:,['A','C']]) 39 | 40 | 41 | # ### Example 3 42 | # 43 | 44 | # In[3]: 45 | 46 | 47 | # import the pandas library and aliasing as pd 48 | import pandas as pd 49 | import numpy as np 50 | 51 | df = pd.DataFrame(np.random.randn(8, 4), 52 | index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) 53 | 54 | # Select few rows for multiple columns, say list[] 55 | print (df.loc[['a','b','f','h'],['A','C']]) 56 | 57 | 58 | # ### Example 4 59 | # 60 | 61 | # In[4]: 62 | 63 | 64 | # import the pandas library and aliasing as pd 65 | import pandas as pd 66 | import numpy as np 67 | 68 | df = pd.DataFrame(np.random.randn(8, 4), 69 | index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) 70 | 71 | # Select range of rows for all columns 72 | print (df.loc['a':'h']) 73 | 74 | 75 | # ### Example 5 76 | # 77 | 78 | # In[5]: 79 | 80 | 81 | # import the pandas library and aliasing as pd 82 | import pandas as pd 83 | import numpy as np 84 | 85 | df = pd.DataFrame(np.random.randn(8, 4), 86 | index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) 87 | 88 | # for getting values with a boolean array 89 | print (df.loc['a']>0) 90 | 91 | 92 | # ## .iloc() 93 | # 94 | 95 | # ### Example 1 96 | # 97 | 98 | # In[6]: 99 | 100 | 101 | # import the pandas library and aliasing as pd 102 | import pandas as pd 103 | import numpy as np 104 | 105 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 106 | 107 | # select all rows for a specific column 108 | print (df.iloc[:4]) 109 | 110 | 111 | # ### Example 2 112 | # 113 | 114 | # In[7]: 115 | 116 | 117 | import pandas as pd 118 | import numpy as np 119 | 120 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 121 | 122 | # Integer slicing 123 | print (df.iloc[:4]) 124 | print (df.iloc[1:5, 2:4]) 125 | 126 | 127 | # ### Example 3 128 | # 129 | 130 | # In[8]: 131 | 132 | 133 | import pandas as pd 134 | import numpy as np 135 | 136 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 137 | 138 | # Slicing through list of values 139 | print (df.iloc[[1, 3, 5], [1, 3]]) 140 | print (df.iloc[1:3, :]) 141 | print (df.iloc[:,1:3]) 142 | 143 | 144 | # ## Use of Notations 145 | 146 | # ### Example 1 147 | # 148 | 149 | # In[12]: 150 | 151 | 152 | import pandas as pd 153 | import numpy as np 154 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 155 | print (df['A']) 156 | 157 | 158 | # ### Example 2 159 | # 160 | 161 | # In[13]: 162 | 163 | 164 | import pandas as pd 165 | import numpy as np 166 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 167 | 168 | print (df[['A','B']]) 169 | 170 | 171 | # ### Example 3 172 | # 173 | 174 | # In[14]: 175 | 176 | 177 | import pandas as pd 178 | import numpy as np 179 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 180 | print (df[2:2]) 181 | 182 | 183 | # ### Attribute Access 184 | # 185 | 186 | # In[15]: 187 | 188 | 189 | import pandas as pd 190 | import numpy as np 191 | df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) 192 | 193 | print (df.A) 194 | 195 | 196 | # In[ ]: 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.11-Statistical Functions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Percent_change 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import pandas as pd 11 | import numpy as np 12 | s = pd.Series([1,2,3,4,5,4]) 13 | print (s.pct_change()) 14 | 15 | df = pd.DataFrame(np.random.randn(5, 2)) 16 | print (df.pct_change()) 17 | 18 | 19 | # ### Covariance 20 | # 21 | 22 | # In[2]: 23 | 24 | 25 | import pandas as pd 26 | import numpy as np 27 | s1 = pd.Series(np.random.randn(10)) 28 | s2 = pd.Series(np.random.randn(10)) 29 | print (s1.cov(s2)) 30 | 31 | 32 | # In[3]: 33 | 34 | 35 | import pandas as pd 36 | import numpy as np 37 | frame = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e']) 38 | print (frame['a'].cov(frame['b'])) 39 | print (frame.cov()) 40 | 41 | 42 | # ### Correlation 43 | # 44 | 45 | # In[4]: 46 | 47 | 48 | import pandas as pd 49 | import numpy as np 50 | frame = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e']) 51 | 52 | print (frame['a'].corr(frame['b'])) 53 | print (frame.corr()) 54 | 55 | 56 | # ### Data Ranking 57 | # 58 | 59 | # In[6]: 60 | 61 | 62 | import pandas as pd 63 | import numpy as nm 64 | 65 | s = pd.Series(np.random.randn(5), index=list('abcde')) 66 | s['d'] = s['b'] # so there's a tie 67 | print (s.rank()) 68 | 69 | 70 | # ### .rolling() Function 71 | # 72 | 73 | # In[8]: 74 | 75 | 76 | import pandas as pd 77 | import numpy as np 78 | 79 | df = pd.DataFrame(np.random.randn(10, 4), 80 | index = pd.date_range('1/1/2000', periods=10), 81 | columns = ['A', 'B', 'C', 'D']) 82 | print (df.rolling(window=3).mean()) 83 | 84 | 85 | # ### .expanding() Function 86 | # 87 | 88 | # In[9]: 89 | 90 | 91 | import pandas as pd 92 | import numpy as np 93 | 94 | df = pd.DataFrame(np.random.randn(10, 4), 95 | index = pd.date_range('1/1/2000', periods=10), 96 | columns = ['A', 'B', 'C', 'D']) 97 | print (df.expanding(min_periods=3).mean()) 98 | 99 | 100 | # ### .ewm() Function 101 | # 102 | 103 | # In[10]: 104 | 105 | 106 | import pandas as pd 107 | import numpy as np 108 | 109 | df = pd.DataFrame(np.random.randn(10, 4), 110 | index = pd.date_range('1/1/2000', periods=10), 111 | columns = ['A', 'B', 'C', 'D']) 112 | print (df.ewm(com=0.5).mean()) 113 | 114 | 115 | # In[ ]: 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.12-Aggregation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Applying Aggregations on DataFrame 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import pandas as pd 11 | import numpy as np 12 | 13 | df = pd.DataFrame(np.random.randn(10, 4), 14 | index = pd.date_range('1/1/2000', periods=10), 15 | columns = ['A', 'B', 'C', 'D']) 16 | 17 | print (df) 18 | r = df.rolling(window=3,min_periods=1) 19 | print (r) 20 | 21 | 22 | # ### Apply Aggregation on a Whole Dataframe 23 | # 24 | 25 | # In[2]: 26 | 27 | 28 | import pandas as pd 29 | import numpy as np 30 | 31 | df = pd.DataFrame(np.random.randn(10, 4), 32 | index = pd.date_range('1/1/2000', periods=10), 33 | columns = ['A', 'B', 'C', 'D']) 34 | print (df) 35 | r = df.rolling(window=3,min_periods=1) 36 | print (r.aggregate(np.sum)) 37 | 38 | 39 | # ### Apply Aggregation on a Single Column of a Dataframe 40 | # 41 | 42 | # In[4]: 43 | 44 | 45 | import pandas as pd 46 | import numpy as np 47 | 48 | df = pd.DataFrame(np.random.randn(10, 4), 49 | index = pd.date_range('1/1/2000', periods=10), 50 | columns = ['A', 'B', 'C', 'D']) 51 | print (df) 52 | r = df.rolling(window=3,min_periods=1) 53 | print (r['A'].aggregate(np.sum)) 54 | 55 | 56 | # ### Apply Aggregation on Multiple Columns of a DataFrame 57 | 58 | # In[5]: 59 | 60 | 61 | import pandas as pd 62 | import numpy as np 63 | 64 | df = pd.DataFrame(np.random.randn(10, 4), 65 | index = pd.date_range('1/1/2000', periods=10), 66 | columns = ['A', 'B', 'C', 'D']) 67 | print (df) 68 | r = df.rolling(window=3,min_periods=1) 69 | print (r[['A','B']].aggregate(np.sum)) 70 | 71 | 72 | # ### Apply Multiple Functions on a Single Column of a DataFrame 73 | # 74 | 75 | # In[6]: 76 | 77 | 78 | import pandas as pd 79 | import numpy as np 80 | 81 | df = pd.DataFrame(np.random.randn(10, 4), 82 | index = pd.date_range('1/1/2000', periods=10), 83 | columns = ['A', 'B', 'C', 'D']) 84 | print (df) 85 | r = df.rolling(window=3,min_periods=1) 86 | print (r['A'].aggregate([np.sum,np.mean])) 87 | 88 | 89 | # ### Apply Multiple Functions on Multiple Columns of a DataFrame 90 | # 91 | 92 | # In[7]: 93 | 94 | 95 | import pandas as pd 96 | import numpy as np 97 | 98 | df = pd.DataFrame(np.random.randn(10, 4), 99 | index = pd.date_range('1/1/2000', periods=10), 100 | columns = ['A', 'B', 'C', 'D']) 101 | print (df) 102 | r = df.rolling(window=3,min_periods=1) 103 | print (r[['A','B']].aggregate([np.sum,np.mean])) 104 | 105 | 106 | # ### Apply Different Functions to Different Columns of a Dataframe 107 | # 108 | 109 | # In[8]: 110 | 111 | 112 | import pandas as pd 113 | import numpy as np 114 | 115 | df = pd.DataFrame(np.random.randn(3, 4), 116 | index = pd.date_range('1/1/2000', periods=3), 117 | columns = ['A', 'B', 'C', 'D']) 118 | print (df) 119 | r = df.rolling(window=3,min_periods=1) 120 | print (r.aggregate({'A' : np.sum,'B' : np.mean})) 121 | 122 | 123 | # In[ ]: 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.13-Missing Data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | # import the pandas library 8 | import pandas as pd 9 | import numpy as np 10 | 11 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 12 | 'h'],columns=['one', 'two', 'three']) 13 | 14 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 15 | 16 | print (df) 17 | 18 | 19 | # ## Check for Missing Values 20 | # 21 | 22 | # ### Example 1 23 | # 24 | 25 | # In[3]: 26 | 27 | 28 | import pandas as pd 29 | import numpy as np 30 | 31 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 32 | 'h'],columns=['one', 'two', 'three']) 33 | 34 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 35 | 36 | print (df['one'].isnull()) 37 | 38 | 39 | # ### Example 2 40 | # 41 | 42 | # In[4]: 43 | 44 | 45 | import pandas as pd 46 | import numpy as np 47 | 48 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 49 | 'h'],columns=['one', 'two', 'three']) 50 | 51 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 52 | 53 | print (df['one'].notnull()) 54 | 55 | 56 | # ## Calculations with Missing Data 57 | # 58 | 59 | # ### Example 1 60 | # 61 | 62 | # In[5]: 63 | 64 | 65 | import pandas as pd 66 | import numpy as np 67 | 68 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 69 | 'h'],columns=['one', 'two', 'three']) 70 | 71 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 72 | 73 | print (df['one'].sum()) 74 | 75 | 76 | # ### Example 2 77 | # 78 | 79 | # In[6]: 80 | 81 | 82 | import pandas as pd 83 | import numpy as np 84 | 85 | df = pd.DataFrame(index=[0,1,2,3,4,5],columns=['one','two']) 86 | print (df['one'].sum()) 87 | 88 | 89 | # ## Replace NaN with a Scalar Value 90 | # 91 | 92 | # In[7]: 93 | 94 | 95 | import pandas as pd 96 | import numpy as np 97 | 98 | df = pd.DataFrame(np.random.randn(3, 3), index=['a', 'c', 'e'],columns=['one', 99 | 'two', 'three']) 100 | 101 | df = df.reindex(['a', 'b', 'c']) 102 | 103 | print (df) 104 | print ("NaN replaced with '0':") 105 | print (df.fillna(0)) 106 | 107 | 108 | # ## Fill NA Forward and Backward 109 | # 110 | 111 | # ### Example 1 112 | # 113 | 114 | # In[8]: 115 | 116 | 117 | import pandas as pd 118 | import numpy as np 119 | 120 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 121 | 'h'],columns=['one', 'two', 'three']) 122 | 123 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 124 | 125 | print (df.fillna(method='pad')) 126 | 127 | 128 | # ### Example 2 129 | # 130 | 131 | # In[9]: 132 | 133 | 134 | import pandas as pd 135 | import numpy as np 136 | 137 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 138 | 'h'],columns=['one', 'two', 'three']) 139 | 140 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 141 | 142 | print (df.fillna(method='backfill')) 143 | 144 | 145 | # ## Drop Missing Values 146 | # 147 | 148 | # ### Example 1 149 | # 150 | 151 | # In[11]: 152 | 153 | 154 | import pandas as pd 155 | import numpy as np 156 | 157 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 158 | 'h'],columns=['one', 'two', 'three']) 159 | 160 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 161 | print (df.dropna()) 162 | 163 | 164 | # ### Example 2 165 | # 166 | 167 | # In[12]: 168 | 169 | 170 | import pandas as pd 171 | import numpy as np 172 | 173 | df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 174 | 'h'],columns=['one', 'two', 'three']) 175 | 176 | df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 177 | print (df.dropna(axis=1)) 178 | 179 | 180 | # ## Replace Missing (or) Generic Values 181 | # 182 | 183 | # ### Example 1 184 | # 185 | 186 | # In[13]: 187 | 188 | 189 | import pandas as pd 190 | import numpy as np 191 | 192 | df = pd.DataFrame({'one':[10,20,30,40,50,2000], 'two':[1000,0,30,40,50,60]}) 193 | 194 | print (df.replace({1000:10,2000:60})) 195 | 196 | 197 | # ### Example 2 198 | # 199 | 200 | # In[14]: 201 | 202 | 203 | import pandas as pd 204 | import numpy as np 205 | 206 | df = pd.DataFrame({'one':[10,20,30,40,50,2000], 'two':[1000,0,30,40,50,60]}) 207 | print (df.replace({1000:10,2000:60})) 208 | 209 | 210 | # In[ ]: 211 | 212 | 213 | 214 | 215 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.14-GroupBy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[16]: 5 | 6 | 7 | #import the pandas library 8 | import pandas as pd 9 | 10 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 11 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 12 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 13 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 14 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 15 | df = pd.DataFrame(ipl_data) 16 | 17 | print (df) 18 | 19 | 20 | # ### Split Data into Groups 21 | # 22 | 23 | # In[17]: 24 | 25 | 26 | # import the pandas library 27 | import pandas as pd 28 | 29 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 30 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 31 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 32 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 33 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 34 | df = pd.DataFrame(ipl_data) 35 | 36 | print (df.groupby('Team')) 37 | 38 | 39 | # ### View Groups 40 | # 41 | 42 | # In[18]: 43 | 44 | 45 | # import the pandas library 46 | import pandas as pd 47 | 48 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 49 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 50 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 51 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 52 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 53 | df = pd.DataFrame(ipl_data) 54 | 55 | print (df.groupby('Team').groups) 56 | 57 | 58 | # ### Example 59 | # 60 | 61 | # In[4]: 62 | 63 | 64 | # import the pandas library 65 | import pandas as pd 66 | 67 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 68 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 69 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 70 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 71 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 72 | df = pd.DataFrame(ipl_data) 73 | 74 | print (df.groupby(['Team','Year']).groups) 75 | 76 | 77 | # ### Iterating through Groups 78 | # 79 | 80 | # In[5]: 81 | 82 | 83 | # import the pandas library 84 | import pandas as pd 85 | 86 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 87 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 88 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 89 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 90 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 91 | df = pd.DataFrame(ipl_data) 92 | 93 | grouped = df.groupby('Year') 94 | 95 | for name,group in grouped: 96 | print (name) 97 | print (group) 98 | 99 | 100 | # ### Select a Group 101 | # 102 | 103 | # In[6]: 104 | 105 | 106 | # import the pandas library 107 | import pandas as pd 108 | 109 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 110 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 111 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 112 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 113 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 114 | df = pd.DataFrame(ipl_data) 115 | 116 | grouped = df.groupby('Year') 117 | print (grouped.get_group(2014)) 118 | 119 | 120 | # ### Aggregations 121 | # 122 | 123 | # In[7]: 124 | 125 | 126 | # import the pandas library 127 | import pandas as pd 128 | import numpy as np 129 | 130 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 131 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 132 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 133 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 134 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 135 | df = pd.DataFrame(ipl_data) 136 | 137 | grouped = df.groupby('Year') 138 | print (grouped['Points'].agg(np.mean)) 139 | 140 | 141 | # ### Applying Multiple Aggregation Functions at Once 142 | # 143 | 144 | # In[12]: 145 | 146 | 147 | # import the pandas library 148 | import pandas as pd 149 | import numpy as np 150 | 151 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 152 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 153 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 154 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 155 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 156 | df = pd.DataFrame(ipl_data) 157 | 158 | grouped = df.groupby('Team') 159 | print (grouped['Points'].agg([np.sum, np.mean, np.std])) 160 | 161 | 162 | # ### Transformations 163 | # 164 | 165 | # In[13]: 166 | 167 | 168 | # import the pandas library 169 | import pandas as pd 170 | import numpy as np 171 | 172 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 173 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 174 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 175 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 176 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 177 | df = pd.DataFrame(ipl_data) 178 | 179 | grouped = df.groupby('Team') 180 | score = lambda x: (x - x.mean()) / x.std()*10 181 | print (grouped.transform(score)) 182 | 183 | 184 | # ### Filtration 185 | # 186 | 187 | # In[15]: 188 | 189 | 190 | import pandas as pd 191 | import numpy as np 192 | 193 | ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings', 194 | 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'], 195 | 'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2], 196 | 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 197 | 'Points':[876,789,863,673,741,812,756,788,694,701,804,690]} 198 | df = pd.DataFrame(ipl_data) 199 | 200 | print (df.groupby('Team').filter(lambda x: len(x) >= 3)) 201 | 202 | 203 | # In[ ]: 204 | 205 | 206 | 207 | 208 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.15-Merging or Joining.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | # import the pandas library 8 | import pandas as pd 9 | left = pd.DataFrame({ 10 | 'id':[1,2,3,4,5], 11 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 12 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 13 | right = pd.DataFrame( 14 | {'id':[1,2,3,4,5], 15 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 16 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 17 | print (left) 18 | print (right) 19 | 20 | 21 | # ### Merge Two DataFrames on a Key 22 | # 23 | 24 | # In[2]: 25 | 26 | 27 | import pandas as pd 28 | left = pd.DataFrame({ 29 | 'id':[1,2,3,4,5], 30 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 31 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 32 | right = pd.DataFrame({ 33 | 'id':[1,2,3,4,5], 34 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 35 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 36 | print (pd.merge(left,right,on='id')) 37 | 38 | 39 | # ### Merge Two DataFrames on Multiple Keys 40 | # 41 | 42 | # In[3]: 43 | 44 | 45 | import pandas as pd 46 | left = pd.DataFrame({ 47 | 'id':[1,2,3,4,5], 48 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 49 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 50 | right = pd.DataFrame({ 51 | 'id':[1,2,3,4,5], 52 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 53 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 54 | print (pd.merge(left,right,on=['id','subject_id'])) 55 | 56 | 57 | # ### Left Join 58 | 59 | # In[4]: 60 | 61 | 62 | import pandas as pd 63 | left = pd.DataFrame({ 64 | 'id':[1,2,3,4,5], 65 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 66 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 67 | right = pd.DataFrame({ 68 | 'id':[1,2,3,4,5], 69 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 70 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 71 | print (pd.merge(left, right, on='subject_id', how='left')) 72 | 73 | 74 | # ### Right Join 75 | # 76 | 77 | # In[5]: 78 | 79 | 80 | import pandas as pd 81 | left = pd.DataFrame({ 82 | 'id':[1,2,3,4,5], 83 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 84 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 85 | right = pd.DataFrame({ 86 | 'id':[1,2,3,4,5], 87 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 88 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 89 | print (pd.merge(left, right, on='subject_id', how='right')) 90 | 91 | 92 | # ### Outer Join 93 | # 94 | 95 | # In[6]: 96 | 97 | 98 | import pandas as pd 99 | left = pd.DataFrame({ 100 | 'id':[1,2,3,4,5], 101 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 102 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 103 | right = pd.DataFrame({ 104 | 'id':[1,2,3,4,5], 105 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 106 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 107 | print (pd.merge(left, right, how='outer', on='subject_id')) 108 | 109 | 110 | # ### Inner Join 111 | # 112 | 113 | # In[7]: 114 | 115 | 116 | import pandas as pd 117 | left = pd.DataFrame({ 118 | 'id':[1,2,3,4,5], 119 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 120 | 'subject_id':['sub1','sub2','sub4','sub6','sub5']}) 121 | right = pd.DataFrame({ 122 | 'id':[1,2,3,4,5], 123 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 124 | 'subject_id':['sub2','sub4','sub3','sub6','sub5']}) 125 | print (pd.merge(left, right, on='subject_id', how='inner')) 126 | 127 | 128 | # In[ ]: 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.16-Concatenation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Concatenating Objects 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import pandas as pd 11 | 12 | one = pd.DataFrame({ 13 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 14 | 'subject_id':['sub1','sub2','sub4','sub6','sub5'], 15 | 'Marks_scored':[98,90,87,69,78]}, 16 | index=[1,2,3,4,5]) 17 | 18 | two = pd.DataFrame({ 19 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 20 | 'subject_id':['sub2','sub4','sub3','sub6','sub5'], 21 | 'Marks_scored':[89,80,79,97,88]}, 22 | index=[1,2,3,4,5]) 23 | print (pd.concat([one,two])) 24 | 25 | 26 | # In[2]: 27 | 28 | 29 | import pandas as pd 30 | 31 | one = pd.DataFrame({ 32 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 33 | 'subject_id':['sub1','sub2','sub4','sub6','sub5'], 34 | 'Marks_scored':[98,90,87,69,78]}, 35 | index=[1,2,3,4,5]) 36 | 37 | two = pd.DataFrame({ 38 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 39 | 'subject_id':['sub2','sub4','sub3','sub6','sub5'], 40 | 'Marks_scored':[89,80,79,97,88]}, 41 | index=[1,2,3,4,5]) 42 | print (pd.concat([one,two],keys=['x','y'])) 43 | 44 | 45 | # In[3]: 46 | 47 | 48 | import pandas as pd 49 | 50 | one = pd.DataFrame({ 51 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 52 | 'subject_id':['sub1','sub2','sub4','sub6','sub5'], 53 | 'Marks_scored':[98,90,87,69,78]}, 54 | index=[1,2,3,4,5]) 55 | 56 | two = pd.DataFrame({ 57 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 58 | 'subject_id':['sub2','sub4','sub3','sub6','sub5'], 59 | 'Marks_scored':[89,80,79,97,88]}, 60 | index=[1,2,3,4,5]) 61 | print (pd.concat([one,two],keys=['x','y'],ignore_index=True)) 62 | 63 | 64 | # In[4]: 65 | 66 | 67 | import pandas as pd 68 | 69 | one = pd.DataFrame({ 70 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 71 | 'subject_id':['sub1','sub2','sub4','sub6','sub5'], 72 | 'Marks_scored':[98,90,87,69,78]}, 73 | index=[1,2,3,4,5]) 74 | 75 | two = pd.DataFrame({ 76 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 77 | 'subject_id':['sub2','sub4','sub3','sub6','sub5'], 78 | 'Marks_scored':[89,80,79,97,88]}, 79 | index=[1,2,3,4,5]) 80 | print (pd.concat([one,two],axis=1)) 81 | 82 | 83 | # ### Concatenating Using append 84 | # 85 | 86 | # In[5]: 87 | 88 | 89 | import pandas as pd 90 | 91 | one = pd.DataFrame({ 92 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 93 | 'subject_id':['sub1','sub2','sub4','sub6','sub5'], 94 | 'Marks_scored':[98,90,87,69,78]}, 95 | index=[1,2,3,4,5]) 96 | 97 | two = pd.DataFrame({ 98 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 99 | 'subject_id':['sub2','sub4','sub3','sub6','sub5'], 100 | 'Marks_scored':[89,80,79,97,88]}, 101 | index=[1,2,3,4,5]) 102 | print (one.append(two)) 103 | 104 | 105 | # In[6]: 106 | 107 | 108 | import pandas as pd 109 | 110 | one = pd.DataFrame({ 111 | 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 112 | 'subject_id':['sub1','sub2','sub4','sub6','sub5'], 113 | 'Marks_scored':[98,90,87,69,78]}, 114 | index=[1,2,3,4,5]) 115 | 116 | two = pd.DataFrame({ 117 | 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 118 | 'subject_id':['sub2','sub4','sub3','sub6','sub5'], 119 | 'Marks_scored':[89,80,79,97,88]}, 120 | index=[1,2,3,4,5]) 121 | print (one.append([two,one,two])) 122 | 123 | 124 | # ## Time Series 125 | # 126 | 127 | # ### Create a TimeStamp 128 | 129 | # In[9]: 130 | 131 | 132 | import pandas as pd 133 | 134 | print (pd.Timestamp('2017-03-01')) 135 | 136 | 137 | # In[10]: 138 | 139 | 140 | import pandas as pd 141 | 142 | print (pd.Timestamp(1587687255,unit='s')) 143 | 144 | 145 | # ### Create a Range of Time 146 | # 147 | 148 | # In[11]: 149 | 150 | 151 | import pandas as pd 152 | 153 | print (pd.date_range("11:00", "13:30", freq="30min").time) 154 | 155 | 156 | # ### Change the Frequency of Time 157 | # 158 | 159 | # In[12]: 160 | 161 | 162 | import pandas as pd 163 | 164 | print (pd.date_range("11:00", "13:30", freq="H").time) 165 | 166 | 167 | # ### Converting to Timestamps 168 | # 169 | 170 | # In[13]: 171 | 172 | 173 | import pandas as pd 174 | 175 | print (pd.to_datetime(pd.Series(['Jul 31, 2009','2010-01-10', None]))) 176 | 177 | 178 | # In[14]: 179 | 180 | 181 | import pandas as pd 182 | 183 | print (pd.to_datetime(['2005/11/23', '2010.12.31', None])) 184 | 185 | 186 | # In[ ]: 187 | 188 | 189 | 190 | 191 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.17-Data Functionality.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Create a Range of Dates 5 | # 6 | 7 | # In[2]: 8 | 9 | 10 | import pandas as pd 11 | 12 | print (pd.date_range('1/1/2011', periods=5)) 13 | 14 | 15 | # ### Change the Date Frequency 16 | # 17 | 18 | # In[5]: 19 | 20 | 21 | import pandas as pd 22 | 23 | print (pd.date_range('1/1/2011', periods=5,freq='M')) 24 | 25 | 26 | # ### bdate_range 27 | # 28 | 29 | # In[3]: 30 | 31 | 32 | import pandas as pd 33 | 34 | print (pd.date_range('1/1/2011', periods=5)) 35 | 36 | 37 | # ### String 38 | 39 | # In[6]: 40 | 41 | 42 | import pandas as pd 43 | 44 | print (pd.Timedelta('2 days 2 hours 15 minutes 30 seconds')) 45 | 46 | 47 | # ### Integer 48 | # 49 | 50 | # In[7]: 51 | 52 | 53 | import pandas as pd 54 | 55 | print (pd.Timedelta(6,unit='h')) 56 | 57 | 58 | # ### Data Offsets 59 | # 60 | 61 | # In[8]: 62 | 63 | 64 | import pandas as pd 65 | 66 | print (pd.Timedelta(days=2)) 67 | 68 | 69 | # ### to_timedelta() 70 | # 71 | 72 | # In[9]: 73 | 74 | 75 | import pandas as pd 76 | 77 | print (pd.Timedelta(days=2)) 78 | 79 | 80 | # ### Operations 81 | # 82 | 83 | # In[10]: 84 | 85 | 86 | import pandas as pd 87 | 88 | s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D')) 89 | td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ]) 90 | df = pd.DataFrame(dict(A = s, B = td)) 91 | 92 | print (df) 93 | 94 | 95 | # ### Addition Operations 96 | # 97 | 98 | # In[11]: 99 | 100 | 101 | import pandas as pd 102 | 103 | s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D')) 104 | td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ]) 105 | df = pd.DataFrame(dict(A = s, B = td)) 106 | df['C']=df['A']+df['B'] 107 | 108 | print (df) 109 | 110 | 111 | # ### Subtraction Operation 112 | # 113 | 114 | # In[12]: 115 | 116 | 117 | import pandas as pd 118 | 119 | s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D')) 120 | td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ]) 121 | df = pd.DataFrame(dict(A = s, B = td)) 122 | df['C']=df['A']+df['B'] 123 | df['D']=df['C']+df['B'] 124 | 125 | print (df) 126 | 127 | 128 | # In[ ]: 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.18-Categorical Data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Object Creation 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import pandas as pd 11 | 12 | s = pd.Series(["a","b","c","a"], dtype="category") 13 | print (s) 14 | 15 | 16 | # ### pd.Categorical 17 | # 18 | 19 | # In[2]: 20 | 21 | 22 | import pandas as pd 23 | 24 | cat = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c']) 25 | print (cat) 26 | 27 | 28 | # In[3]: 29 | 30 | 31 | import pandas as pd 32 | 33 | cat = cat=pd.Categorical(['a','b','c','a','b','c','d'], ['c', 'b', 'a']) 34 | print (cat) 35 | 36 | 37 | # In[4]: 38 | 39 | 40 | import pandas as pd 41 | 42 | cat = cat=pd.Categorical(['a','b','c','a','b','c','d'], ['c', 'b', 'a'],ordered=True) 43 | print (cat) 44 | 45 | 46 | # ### Description 47 | # 48 | 49 | # In[5]: 50 | 51 | 52 | import pandas as pd 53 | import numpy as np 54 | 55 | cat = pd.Categorical(["a", "c", "c", np.nan], categories=["b", "a", "c"]) 56 | df = pd.DataFrame({"cat":cat, "s":["a", "c", "c", np.nan]}) 57 | 58 | print (df.describe()) 59 | print (df["cat"].describe()) 60 | 61 | 62 | # ### Get the Properties of the Category 63 | # 64 | 65 | # In[6]: 66 | 67 | 68 | import pandas as pd 69 | import numpy as np 70 | 71 | s = pd.Categorical(["a", "c", "c", np.nan], categories=["b", "a", "c"]) 72 | print (s.categories) 73 | 74 | 75 | # In[7]: 76 | 77 | 78 | import pandas as pd 79 | import numpy as np 80 | 81 | cat = pd.Categorical(["a", "c", "c", np.nan], categories=["b", "a", "c"]) 82 | print (cat.ordered) 83 | 84 | 85 | # ### Renaming Categories 86 | # 87 | 88 | # In[8]: 89 | 90 | 91 | import pandas as pd 92 | 93 | s = pd.Series(["a","b","c","a"], dtype="category") 94 | s.cat.categories = ["Group %s" % g for g in s.cat.categories] 95 | print (s.cat.categories) 96 | 97 | 98 | # ### Appending New Categories 99 | # 100 | 101 | # In[9]: 102 | 103 | 104 | import pandas as pd 105 | 106 | s = pd.Series(["a","b","c","a"], dtype="category") 107 | s = s.cat.add_categories([4]) 108 | print (s.cat.categories) 109 | 110 | 111 | # ### Removing Categories 112 | # 113 | 114 | # In[10]: 115 | 116 | 117 | import pandas as pd 118 | 119 | s = pd.Series(["a","b","c","a"], dtype="category") 120 | print ("Original object:") 121 | print (s) 122 | 123 | print ("After removal:") 124 | print (s.cat.remove_categories("a")) 125 | 126 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.19-Visualization.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Basic Plotting: plot 5 | # 6 | 7 | # In[1]: 8 | 9 | 10 | import pandas as pd 11 | import numpy as np 12 | 13 | df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range('1/1/2000', 14 | periods=10), columns=list('ABCD')) 15 | 16 | df.plot() 17 | 18 | 19 | # ### Bar Plot 20 | # 21 | 22 | # In[5]: 23 | 24 | 25 | import pandas as pd 26 | import numpy as np 27 | 28 | df = pd.DataFrame(np.random.rand(10,4),columns=('a','b','c','d')) 29 | df.plot.bar() 30 | 31 | 32 | # In[6]: 33 | 34 | 35 | import pandas as pd 36 | df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d']) 37 | df.plot.bar(stacked=True) 38 | 39 | 40 | # In[7]: 41 | 42 | 43 | import pandas as pd 44 | import numpy as np 45 | 46 | df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d']) 47 | 48 | df.plot.barh(stacked=True) 49 | 50 | 51 | # ### Histograms 52 | # 53 | 54 | # In[8]: 55 | 56 | 57 | import pandas as pd 58 | import numpy as np 59 | 60 | df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c': 61 | np.random.randn(1000) - 1}, columns=['a', 'b', 'c']) 62 | 63 | df.plot.hist(bins=20) 64 | 65 | 66 | # In[22]: 67 | 68 | 69 | import pandas as pd 70 | import numpy as np 71 | 72 | df=pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c': 73 | np.random.randn(1000) - 1,'d':np.random.randn(1000)-2}, columns=['a', 'b', 'c','d']) 74 | 75 | df.hist(bins=20) 76 | 77 | 78 | # In[19]: 79 | 80 | 81 | import pandas as pd 82 | import numpy as np 83 | df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) 84 | df.plot.box() 85 | 86 | 87 | # In[15]: 88 | 89 | 90 | import pandas as pd 91 | import numpy as np 92 | 93 | df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd']) 94 | df.plot.area() 95 | 96 | 97 | # In[16]: 98 | 99 | 100 | import pandas as pd 101 | import numpy as np 102 | df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd']) 103 | df.plot.scatter(x='a', y='b') 104 | 105 | 106 | # In[17]: 107 | 108 | 109 | import pandas as pd 110 | import numpy as np 111 | 112 | df = pd.DataFrame(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], columns=['x']) 113 | df.plot.pie(subplots=True) 114 | 115 | 116 | # In[ ]: 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.2-DataFrame.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## Pandas - DataFrame 5 | 6 | # ### Create DataFrame 7 | # ##### A pandas DataFrame can be created using various inputs like − 8 | # 9 | # - Lists 10 | # - dict 11 | # - Series 12 | # - Numpy ndarrays 13 | # - Another DataFrame 14 | 15 | # ### Create an Empty DataFrame 16 | 17 | # In[1]: 18 | 19 | 20 | #import the pandas library and aliasing as pd 21 | import pandas as pd 22 | df = pd.DataFrame() 23 | print (df) 24 | 25 | 26 | # ### Create a DataFrame from Lists 27 | 28 | # #### Example 1 29 | 30 | # In[2]: 31 | 32 | 33 | import pandas as pd 34 | data = [1,2,3,4,5] 35 | df = pd.DataFrame(data) 36 | print (df) 37 | 38 | 39 | # #### Example 2 40 | 41 | # In[3]: 42 | 43 | 44 | import pandas as pd 45 | data = [['Alex',10],['Bob',12],['Clarke',13]] 46 | df = pd.DataFrame(data,columns=['Name','Age']) 47 | print (df) 48 | 49 | 50 | # #### Example 3 51 | 52 | # In[4]: 53 | 54 | 55 | import pandas as pd 56 | data = [['Alex',10],['Bob',12],['Clarke',13]] 57 | df = pd.DataFrame(data,columns=['Name','Age'],dtype=float) 58 | print (df) 59 | 60 | 61 | # ### Create a DataFrame from Dict of ndarrays / Lists 62 | 63 | # #### Example 1 64 | 65 | # In[5]: 66 | 67 | 68 | import pandas as pd 69 | data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]} 70 | df = pd.DataFrame(data) 71 | print (df) 72 | 73 | 74 | # #### Example 2 75 | 76 | # In[6]: 77 | 78 | 79 | import pandas as pd 80 | data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]} 81 | df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4']) 82 | print (df) 83 | 84 | 85 | # ### Create a DataFrame from List of Dicts 86 | 87 | # #### Example 1 88 | 89 | # In[7]: 90 | 91 | 92 | import pandas as pd 93 | data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] 94 | df = pd.DataFrame(data) 95 | print (df) 96 | 97 | 98 | # #### Example 2 99 | 100 | # In[8]: 101 | 102 | 103 | import pandas as pd 104 | data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] 105 | df = pd.DataFrame(data, index=['first', 'second']) 106 | print (df) 107 | 108 | 109 | # #### Example 3 110 | 111 | # In[9]: 112 | 113 | 114 | import pandas as pd 115 | data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] 116 | 117 | #With two column indices, values same as dictionary keys 118 | df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b']) 119 | 120 | #With two column indices with one index with other name 121 | df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1']) 122 | print (df1) 123 | print (df2) 124 | 125 | 126 | # ### Create a DataFrame from Dict of Series 127 | 128 | # In[10]: 129 | 130 | 131 | import pandas as pd 132 | 133 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 134 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} 135 | 136 | df = pd.DataFrame(d) 137 | print (df) 138 | 139 | 140 | # ### Column Selection 141 | 142 | # In[11]: 143 | 144 | 145 | import pandas as pd 146 | 147 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 148 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} 149 | 150 | df = pd.DataFrame(d) 151 | print (df ['one']) 152 | 153 | 154 | # ### Column Addition 155 | 156 | # In[12]: 157 | 158 | 159 | import pandas as pd 160 | 161 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 162 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} 163 | 164 | df = pd.DataFrame(d) 165 | 166 | # Adding a new column to an existing DataFrame object with column label by passing new series 167 | 168 | print ("Adding a new column by passing as Series:") 169 | df['three']=pd.Series([10,20,30],index=['a','b','c']) 170 | print (df) 171 | 172 | print ("Adding a new column using the existing columns in DataFrame:") 173 | df['four']=df['one']+df['three'] 174 | 175 | print (df) 176 | 177 | 178 | # ### Column Deletion 179 | 180 | # In[13]: 181 | 182 | 183 | # Using the previous DataFrame, we will delete a column 184 | # using del function 185 | import pandas as pd 186 | 187 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 188 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']), 189 | 'three' : pd.Series([10,20,30], index=['a','b','c'])} 190 | 191 | df = pd.DataFrame(d) 192 | print ("Our dataframe is:") 193 | print (df) 194 | 195 | # using del function 196 | print ("Deleting the first column using DEL function:") 197 | del df['one'] 198 | print (df) 199 | 200 | # using pop function 201 | print ("Deleting another column using POP function:") 202 | df.pop('two') 203 | print (df) 204 | 205 | 206 | # ### Row Selection, Addition, and Deletion 207 | 208 | # #### Selection by Label 209 | 210 | # In[14]: 211 | 212 | 213 | import pandas as pd 214 | 215 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 216 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} 217 | 218 | df = pd.DataFrame(d) 219 | print (df.loc['b']) 220 | 221 | 222 | # #### Selection by integer location 223 | # 224 | 225 | # In[15]: 226 | 227 | 228 | import pandas as pd 229 | 230 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 231 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} 232 | 233 | df = pd.DataFrame(d) 234 | print (df.iloc[2]) 235 | 236 | 237 | # #### Slice Rows 238 | 239 | # In[16]: 240 | 241 | 242 | import pandas as pd 243 | 244 | d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 245 | 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} 246 | 247 | df = pd.DataFrame(d) 248 | print (df[2:4]) 249 | 250 | 251 | # #### Addition of Rows 252 | 253 | # In[17]: 254 | 255 | 256 | import pandas as pd 257 | 258 | df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b']) 259 | df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) 260 | 261 | df = df.append(df2) 262 | print (df) 263 | 264 | 265 | # #### Deletion of Rows 266 | 267 | # In[18]: 268 | 269 | 270 | import pandas as pd 271 | 272 | df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b']) 273 | df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) 274 | 275 | df = df.append(df2) 276 | 277 | # Drop rows with label 0 278 | df = df.drop(0) 279 | 280 | print (df) 281 | 282 | 283 | # In[ ]: 284 | 285 | 286 | 287 | 288 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.20-Comparison with SQL/Comparison with SQL.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[19]: 5 | 6 | 7 | import pandas as pd 8 | 9 | tips =pd.read_csv("tips.csv") 10 | tips.head() 11 | 12 | 13 | # ### SELECT 14 | # 15 | 16 | # In[15]: 17 | 18 | 19 | print (tips[['total_bill', 'tip', 'smoker', 'time']].head(5)) 20 | 21 | 22 | # ### WHERE 23 | # 24 | 25 | # In[16]: 26 | 27 | 28 | print (tips[tips['time'] == 'Dinner'].head(5)) 29 | 30 | 31 | # ### GroupBy 32 | # 33 | 34 | # In[17]: 35 | 36 | 37 | print (tips.groupby('sex').size()) 38 | 39 | 40 | # ### Top N rows 41 | # 42 | 43 | # In[18]: 44 | 45 | 46 | tips = tips[['smoker', 'day', 'time']].head(5) 47 | print (tips) 48 | 49 | 50 | # In[ ]: 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.20-Comparison with SQL/tips.csv: -------------------------------------------------------------------------------- 1 | total_bill,tip,sex,smoker,day,time,size 2 | 16.99,1.01,Female,No,Sun,Dinner,2 3 | 10.34,1.66,Male,No,Sun,Dinner,3 4 | 21.01,3.5,Male,No,Sun,Dinner,3 5 | 23.68,3.31,Male,No,Sun,Dinner,2 6 | 24.59,3.61,Female,No,Sun,Dinner,4 7 | 25.29,4.71,Male,No,Sun,Dinner,4 8 | 8.77,2.0,Male,No,Sun,Dinner,2 9 | 26.88,3.12,Male,No,Sun,Dinner,4 10 | 15.04,1.96,Male,No,Sun,Dinner,2 11 | 14.78,3.23,Male,No,Sun,Dinner,2 12 | 10.27,1.71,Male,No,Sun,Dinner,2 13 | 35.26,5.0,Female,No,Sun,Dinner,4 14 | 15.42,1.57,Male,No,Sun,Dinner,2 15 | 18.43,3.0,Male,No,Sun,Dinner,4 16 | 14.83,3.02,Female,No,Sun,Dinner,2 17 | 21.58,3.92,Male,No,Sun,Dinner,2 18 | 10.33,1.67,Female,No,Sun,Dinner,3 19 | 16.29,3.71,Male,No,Sun,Dinner,3 20 | 16.97,3.5,Female,No,Sun,Dinner,3 21 | 20.65,3.35,Male,No,Sat,Dinner,3 22 | 17.92,4.08,Male,No,Sat,Dinner,2 23 | 20.29,2.75,Female,No,Sat,Dinner,2 24 | 15.77,2.23,Female,No,Sat,Dinner,2 25 | 39.42,7.58,Male,No,Sat,Dinner,4 26 | 19.82,3.18,Male,No,Sat,Dinner,2 27 | 17.81,2.34,Male,No,Sat,Dinner,4 28 | 13.37,2.0,Male,No,Sat,Dinner,2 29 | 12.69,2.0,Male,No,Sat,Dinner,2 30 | 21.7,4.3,Male,No,Sat,Dinner,2 31 | 19.65,3.0,Female,No,Sat,Dinner,2 32 | 9.55,1.45,Male,No,Sat,Dinner,2 33 | 18.35,2.5,Male,No,Sat,Dinner,4 34 | 15.06,3.0,Female,No,Sat,Dinner,2 35 | 20.69,2.45,Female,No,Sat,Dinner,4 36 | 17.78,3.27,Male,No,Sat,Dinner,2 37 | 24.06,3.6,Male,No,Sat,Dinner,3 38 | 16.31,2.0,Male,No,Sat,Dinner,3 39 | 16.93,3.07,Female,No,Sat,Dinner,3 40 | 18.69,2.31,Male,No,Sat,Dinner,3 41 | 31.27,5.0,Male,No,Sat,Dinner,3 42 | 16.04,2.24,Male,No,Sat,Dinner,3 43 | 17.46,2.54,Male,No,Sun,Dinner,2 44 | 13.94,3.06,Male,No,Sun,Dinner,2 45 | 9.68,1.32,Male,No,Sun,Dinner,2 46 | 30.4,5.6,Male,No,Sun,Dinner,4 47 | 18.29,3.0,Male,No,Sun,Dinner,2 48 | 22.23,5.0,Male,No,Sun,Dinner,2 49 | 32.4,6.0,Male,No,Sun,Dinner,4 50 | 28.55,2.05,Male,No,Sun,Dinner,3 51 | 18.04,3.0,Male,No,Sun,Dinner,2 52 | 12.54,2.5,Male,No,Sun,Dinner,2 53 | 10.29,2.6,Female,No,Sun,Dinner,2 54 | 34.81,5.2,Female,No,Sun,Dinner,4 55 | 9.94,1.56,Male,No,Sun,Dinner,2 56 | 25.56,4.34,Male,No,Sun,Dinner,4 57 | 19.49,3.51,Male,No,Sun,Dinner,2 58 | 38.01,3.0,Male,Yes,Sat,Dinner,4 59 | 26.41,1.5,Female,No,Sat,Dinner,2 60 | 11.24,1.76,Male,Yes,Sat,Dinner,2 61 | 48.27,6.73,Male,No,Sat,Dinner,4 62 | 20.29,3.21,Male,Yes,Sat,Dinner,2 63 | 13.81,2.0,Male,Yes,Sat,Dinner,2 64 | 11.02,1.98,Male,Yes,Sat,Dinner,2 65 | 18.29,3.76,Male,Yes,Sat,Dinner,4 66 | 17.59,2.64,Male,No,Sat,Dinner,3 67 | 20.08,3.15,Male,No,Sat,Dinner,3 68 | 16.45,2.47,Female,No,Sat,Dinner,2 69 | 3.07,1.0,Female,Yes,Sat,Dinner,1 70 | 20.23,2.01,Male,No,Sat,Dinner,2 71 | 15.01,2.09,Male,Yes,Sat,Dinner,2 72 | 12.02,1.97,Male,No,Sat,Dinner,2 73 | 17.07,3.0,Female,No,Sat,Dinner,3 74 | 26.86,3.14,Female,Yes,Sat,Dinner,2 75 | 25.28,5.0,Female,Yes,Sat,Dinner,2 76 | 14.73,2.2,Female,No,Sat,Dinner,2 77 | 10.51,1.25,Male,No,Sat,Dinner,2 78 | 17.92,3.08,Male,Yes,Sat,Dinner,2 79 | 27.2,4.0,Male,No,Thur,Lunch,4 80 | 22.76,3.0,Male,No,Thur,Lunch,2 81 | 17.29,2.71,Male,No,Thur,Lunch,2 82 | 19.44,3.0,Male,Yes,Thur,Lunch,2 83 | 16.66,3.4,Male,No,Thur,Lunch,2 84 | 10.07,1.83,Female,No,Thur,Lunch,1 85 | 32.68,5.0,Male,Yes,Thur,Lunch,2 86 | 15.98,2.03,Male,No,Thur,Lunch,2 87 | 34.83,5.17,Female,No,Thur,Lunch,4 88 | 13.03,2.0,Male,No,Thur,Lunch,2 89 | 18.28,4.0,Male,No,Thur,Lunch,2 90 | 24.71,5.85,Male,No,Thur,Lunch,2 91 | 21.16,3.0,Male,No,Thur,Lunch,2 92 | 28.97,3.0,Male,Yes,Fri,Dinner,2 93 | 22.49,3.5,Male,No,Fri,Dinner,2 94 | 5.75,1.0,Female,Yes,Fri,Dinner,2 95 | 16.32,4.3,Female,Yes,Fri,Dinner,2 96 | 22.75,3.25,Female,No,Fri,Dinner,2 97 | 40.17,4.73,Male,Yes,Fri,Dinner,4 98 | 27.28,4.0,Male,Yes,Fri,Dinner,2 99 | 12.03,1.5,Male,Yes,Fri,Dinner,2 100 | 21.01,3.0,Male,Yes,Fri,Dinner,2 101 | 12.46,1.5,Male,No,Fri,Dinner,2 102 | 11.35,2.5,Female,Yes,Fri,Dinner,2 103 | 15.38,3.0,Female,Yes,Fri,Dinner,2 104 | 44.3,2.5,Female,Yes,Sat,Dinner,3 105 | 22.42,3.48,Female,Yes,Sat,Dinner,2 106 | 20.92,4.08,Female,No,Sat,Dinner,2 107 | 15.36,1.64,Male,Yes,Sat,Dinner,2 108 | 20.49,4.06,Male,Yes,Sat,Dinner,2 109 | 25.21,4.29,Male,Yes,Sat,Dinner,2 110 | 18.24,3.76,Male,No,Sat,Dinner,2 111 | 14.31,4.0,Female,Yes,Sat,Dinner,2 112 | 14.0,3.0,Male,No,Sat,Dinner,2 113 | 7.25,1.0,Female,No,Sat,Dinner,1 114 | 38.07,4.0,Male,No,Sun,Dinner,3 115 | 23.95,2.55,Male,No,Sun,Dinner,2 116 | 25.71,4.0,Female,No,Sun,Dinner,3 117 | 17.31,3.5,Female,No,Sun,Dinner,2 118 | 29.93,5.07,Male,No,Sun,Dinner,4 119 | 10.65,1.5,Female,No,Thur,Lunch,2 120 | 12.43,1.8,Female,No,Thur,Lunch,2 121 | 24.08,2.92,Female,No,Thur,Lunch,4 122 | 11.69,2.31,Male,No,Thur,Lunch,2 123 | 13.42,1.68,Female,No,Thur,Lunch,2 124 | 14.26,2.5,Male,No,Thur,Lunch,2 125 | 15.95,2.0,Male,No,Thur,Lunch,2 126 | 12.48,2.52,Female,No,Thur,Lunch,2 127 | 29.8,4.2,Female,No,Thur,Lunch,6 128 | 8.52,1.48,Male,No,Thur,Lunch,2 129 | 14.52,2.0,Female,No,Thur,Lunch,2 130 | 11.38,2.0,Female,No,Thur,Lunch,2 131 | 22.82,2.18,Male,No,Thur,Lunch,3 132 | 19.08,1.5,Male,No,Thur,Lunch,2 133 | 20.27,2.83,Female,No,Thur,Lunch,2 134 | 11.17,1.5,Female,No,Thur,Lunch,2 135 | 12.26,2.0,Female,No,Thur,Lunch,2 136 | 18.26,3.25,Female,No,Thur,Lunch,2 137 | 8.51,1.25,Female,No,Thur,Lunch,2 138 | 10.33,2.0,Female,No,Thur,Lunch,2 139 | 14.15,2.0,Female,No,Thur,Lunch,2 140 | 16.0,2.0,Male,Yes,Thur,Lunch,2 141 | 13.16,2.75,Female,No,Thur,Lunch,2 142 | 17.47,3.5,Female,No,Thur,Lunch,2 143 | 34.3,6.7,Male,No,Thur,Lunch,6 144 | 41.19,5.0,Male,No,Thur,Lunch,5 145 | 27.05,5.0,Female,No,Thur,Lunch,6 146 | 16.43,2.3,Female,No,Thur,Lunch,2 147 | 8.35,1.5,Female,No,Thur,Lunch,2 148 | 18.64,1.36,Female,No,Thur,Lunch,3 149 | 11.87,1.63,Female,No,Thur,Lunch,2 150 | 9.78,1.73,Male,No,Thur,Lunch,2 151 | 7.51,2.0,Male,No,Thur,Lunch,2 152 | 14.07,2.5,Male,No,Sun,Dinner,2 153 | 13.13,2.0,Male,No,Sun,Dinner,2 154 | 17.26,2.74,Male,No,Sun,Dinner,3 155 | 24.55,2.0,Male,No,Sun,Dinner,4 156 | 19.77,2.0,Male,No,Sun,Dinner,4 157 | 29.85,5.14,Female,No,Sun,Dinner,5 158 | 48.17,5.0,Male,No,Sun,Dinner,6 159 | 25.0,3.75,Female,No,Sun,Dinner,4 160 | 13.39,2.61,Female,No,Sun,Dinner,2 161 | 16.49,2.0,Male,No,Sun,Dinner,4 162 | 21.5,3.5,Male,No,Sun,Dinner,4 163 | 12.66,2.5,Male,No,Sun,Dinner,2 164 | 16.21,2.0,Female,No,Sun,Dinner,3 165 | 13.81,2.0,Male,No,Sun,Dinner,2 166 | 17.51,3.0,Female,Yes,Sun,Dinner,2 167 | 24.52,3.48,Male,No,Sun,Dinner,3 168 | 20.76,2.24,Male,No,Sun,Dinner,2 169 | 31.71,4.5,Male,No,Sun,Dinner,4 170 | 10.59,1.61,Female,Yes,Sat,Dinner,2 171 | 10.63,2.0,Female,Yes,Sat,Dinner,2 172 | 50.81,10.0,Male,Yes,Sat,Dinner,3 173 | 15.81,3.16,Male,Yes,Sat,Dinner,2 174 | 7.25,5.15,Male,Yes,Sun,Dinner,2 175 | 31.85,3.18,Male,Yes,Sun,Dinner,2 176 | 16.82,4.0,Male,Yes,Sun,Dinner,2 177 | 32.9,3.11,Male,Yes,Sun,Dinner,2 178 | 17.89,2.0,Male,Yes,Sun,Dinner,2 179 | 14.48,2.0,Male,Yes,Sun,Dinner,2 180 | 9.6,4.0,Female,Yes,Sun,Dinner,2 181 | 34.63,3.55,Male,Yes,Sun,Dinner,2 182 | 34.65,3.68,Male,Yes,Sun,Dinner,4 183 | 23.33,5.65,Male,Yes,Sun,Dinner,2 184 | 45.35,3.5,Male,Yes,Sun,Dinner,3 185 | 23.17,6.5,Male,Yes,Sun,Dinner,4 186 | 40.55,3.0,Male,Yes,Sun,Dinner,2 187 | 20.69,5.0,Male,No,Sun,Dinner,5 188 | 20.9,3.5,Female,Yes,Sun,Dinner,3 189 | 30.46,2.0,Male,Yes,Sun,Dinner,5 190 | 18.15,3.5,Female,Yes,Sun,Dinner,3 191 | 23.1,4.0,Male,Yes,Sun,Dinner,3 192 | 15.69,1.5,Male,Yes,Sun,Dinner,2 193 | 19.81,4.19,Female,Yes,Thur,Lunch,2 194 | 28.44,2.56,Male,Yes,Thur,Lunch,2 195 | 15.48,2.02,Male,Yes,Thur,Lunch,2 196 | 16.58,4.0,Male,Yes,Thur,Lunch,2 197 | 7.56,1.44,Male,No,Thur,Lunch,2 198 | 10.34,2.0,Male,Yes,Thur,Lunch,2 199 | 43.11,5.0,Female,Yes,Thur,Lunch,4 200 | 13.0,2.0,Female,Yes,Thur,Lunch,2 201 | 13.51,2.0,Male,Yes,Thur,Lunch,2 202 | 18.71,4.0,Male,Yes,Thur,Lunch,3 203 | 12.74,2.01,Female,Yes,Thur,Lunch,2 204 | 13.0,2.0,Female,Yes,Thur,Lunch,2 205 | 16.4,2.5,Female,Yes,Thur,Lunch,2 206 | 20.53,4.0,Male,Yes,Thur,Lunch,4 207 | 16.47,3.23,Female,Yes,Thur,Lunch,3 208 | 26.59,3.41,Male,Yes,Sat,Dinner,3 209 | 38.73,3.0,Male,Yes,Sat,Dinner,4 210 | 24.27,2.03,Male,Yes,Sat,Dinner,2 211 | 12.76,2.23,Female,Yes,Sat,Dinner,2 212 | 30.06,2.0,Male,Yes,Sat,Dinner,3 213 | 25.89,5.16,Male,Yes,Sat,Dinner,4 214 | 48.33,9.0,Male,No,Sat,Dinner,4 215 | 13.27,2.5,Female,Yes,Sat,Dinner,2 216 | 28.17,6.5,Female,Yes,Sat,Dinner,3 217 | 12.9,1.1,Female,Yes,Sat,Dinner,2 218 | 28.15,3.0,Male,Yes,Sat,Dinner,5 219 | 11.59,1.5,Male,Yes,Sat,Dinner,2 220 | 7.74,1.44,Male,Yes,Sat,Dinner,2 221 | 30.14,3.09,Female,Yes,Sat,Dinner,4 222 | 12.16,2.2,Male,Yes,Fri,Lunch,2 223 | 13.42,3.48,Female,Yes,Fri,Lunch,2 224 | 8.58,1.92,Male,Yes,Fri,Lunch,1 225 | 15.98,3.0,Female,No,Fri,Lunch,3 226 | 13.42,1.58,Male,Yes,Fri,Lunch,2 227 | 16.27,2.5,Female,Yes,Fri,Lunch,2 228 | 10.09,2.0,Female,Yes,Fri,Lunch,2 229 | 20.45,3.0,Male,No,Sat,Dinner,4 230 | 13.28,2.72,Male,No,Sat,Dinner,2 231 | 22.12,2.88,Female,Yes,Sat,Dinner,2 232 | 24.01,2.0,Male,Yes,Sat,Dinner,4 233 | 15.69,3.0,Male,Yes,Sat,Dinner,3 234 | 11.61,3.39,Male,No,Sat,Dinner,2 235 | 10.77,1.47,Male,No,Sat,Dinner,2 236 | 15.53,3.0,Male,Yes,Sat,Dinner,2 237 | 10.07,1.25,Male,No,Sat,Dinner,2 238 | 12.6,1.0,Male,Yes,Sat,Dinner,2 239 | 32.83,1.17,Male,Yes,Sat,Dinner,2 240 | 35.83,4.67,Female,No,Sat,Dinner,3 241 | 29.03,5.92,Male,No,Sat,Dinner,3 242 | 27.18,2.0,Female,Yes,Sat,Dinner,2 243 | 22.67,2.0,Male,Yes,Sat,Dinner,2 244 | 17.82,1.75,Male,No,Sat,Dinner,2 245 | 18.78,3.0,Female,No,Thur,Dinner,2 246 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.3-Basic Functionality.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Creating a Series 5 | 6 | # In[3]: 7 | 8 | 9 | import pandas as pd 10 | import numpy as np 11 | 12 | #Create a series with 100 random numbers 13 | s = pd.Series(np.random.randn(4)) 14 | print (s) 15 | 16 | 17 | # ### Axes 18 | 19 | # In[5]: 20 | 21 | 22 | import pandas as pd 23 | import numpy as np 24 | 25 | #Create a series with 100 random numbers 26 | s = pd.Series(np.random.randn(4)) 27 | print ("The axes are:") 28 | print (s.axes) 29 | 30 | 31 | # ### Empty 32 | 33 | # In[7]: 34 | 35 | 36 | import pandas as pd 37 | import numpy as np 38 | 39 | #Create a series with 100 random numbers 40 | s = pd.Series(np.random.randn(4)) 41 | print ("Is the Object empty?") 42 | print (s.empty) 43 | 44 | 45 | # In[9]: 46 | 47 | 48 | import pandas as pd 49 | import numpy as np 50 | 51 | #Create a series with 4 random numbers 52 | s = pd.Series(np.random.randn(4)) 53 | print (s) 54 | 55 | print ("The dimensions of the object:") 56 | print (s.ndim) 57 | 58 | 59 | # ### Size 60 | 61 | # In[10]: 62 | 63 | 64 | import pandas as pd 65 | import numpy as np 66 | 67 | #Create a series with 4 random numbers 68 | s = pd.Series(np.random.randn(2)) 69 | print (s) 70 | print ("The size of the object:") 71 | print (s.size) 72 | 73 | 74 | # ### Values 75 | 76 | # In[11]: 77 | 78 | 79 | import pandas as pd 80 | import numpy as np 81 | 82 | #Create a series with 4 random numbers 83 | s = pd.Series(np.random.randn(4)) 84 | print (s) 85 | 86 | print ("The actual data series is:") 87 | print (s.values) 88 | 89 | 90 | # ### Head & Tail 91 | # 92 | 93 | # In[13]: 94 | 95 | 96 | import pandas as pd 97 | import numpy as np 98 | 99 | #Create a series with 4 random numbers 100 | s = pd.Series(np.random.randn(4)) 101 | print ("The original series is:") 102 | print (s) 103 | 104 | print ("The first two rows of the data series:") 105 | print (s.head(2)) 106 | 107 | 108 | # In[16]: 109 | 110 | 111 | import pandas as pd 112 | import numpy as np 113 | 114 | #Create a series with 4 random numbers 115 | s = pd.Series(np.random.randn(4)) 116 | print ("The original series is:") 117 | print (s) 118 | 119 | print ("The last two rows of the data series:") 120 | print (s.tail(2)) 121 | 122 | 123 | # ### DataFrame Basic Functionality 124 | # 125 | 126 | # In[17]: 127 | 128 | 129 | import pandas as pd 130 | import numpy as np 131 | 132 | #Create a Dictionary of series 133 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 134 | 'Age':pd.Series([25,26,25,23,30,29,23]), 135 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 136 | 137 | #Create a DataFrame 138 | df = pd.DataFrame(d) 139 | print ("Our data series is:") 140 | print (df) 141 | 142 | 143 | # ### T (Transpose) 144 | 145 | # In[18]: 146 | 147 | 148 | import pandas as pd 149 | import numpy as np 150 | 151 | # Create a Dictionary of series 152 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 153 | 'Age':pd.Series([25,26,25,23,30,29,23]), 154 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 155 | 156 | # Create a DataFrame 157 | df = pd.DataFrame(d) 158 | print ("The transpose of the data series is:") 159 | print (df.T) 160 | 161 | 162 | # ### axes 163 | # 164 | 165 | # In[2]: 166 | 167 | 168 | import pandas as pd 169 | import numpy as np 170 | 171 | #Create a Dictionary of series 172 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 173 | 'Age':pd.Series([25,26,25,23,30,29,23]), 174 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 175 | 176 | #Create a DataFrame 177 | df = pd.DataFrame(d) 178 | print ("Row axis labels and column axis labels are:") 179 | print (df.axes) 180 | 181 | 182 | # ### dtypes 183 | # 184 | 185 | # In[4]: 186 | 187 | 188 | import pandas as pd 189 | import numpy as np 190 | 191 | #Create a Dictionary of series 192 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 193 | 'Age':pd.Series([25,26,25,23,30,29,23]), 194 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 195 | 196 | #Create a DataFrame 197 | df = pd.DataFrame(d) 198 | print ("The data types of each column are:") 199 | print (df.dtypes) 200 | 201 | 202 | # ### empty 203 | # 204 | 205 | # In[5]: 206 | 207 | 208 | import pandas as pd 209 | import numpy as np 210 | 211 | #Create a Dictionary of series 212 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 213 | 'Age':pd.Series([25,26,25,23,30,29,23]), 214 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 215 | 216 | #Create a DataFrame 217 | df = pd.DataFrame(d) 218 | print ("Is the object empty?") 219 | print (df.empty) 220 | 221 | 222 | # ### ndim 223 | # 224 | 225 | # In[7]: 226 | 227 | 228 | import pandas as pd 229 | import numpy as np 230 | 231 | #Create a Dictionary of series 232 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 233 | 'Age':pd.Series([25,26,25,23,30,29,23]), 234 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 235 | 236 | #Create a DataFrame 237 | df = pd.DataFrame(d) 238 | print ("Our object is:") 239 | print (df) 240 | print ("The dimension of the object is:") 241 | print (df.ndim) 242 | 243 | 244 | # ### shape 245 | # 246 | 247 | # In[9]: 248 | 249 | 250 | import pandas as pd 251 | import numpy as np 252 | 253 | #Create a Dictionary of series 254 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 255 | 'Age':pd.Series([25,26,25,23,30,29,23]), 256 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 257 | 258 | #Create a DataFrame 259 | df = pd.DataFrame(d) 260 | print ("Our object is:") 261 | print (df) 262 | print ("The shape of the object is:") 263 | print (df.shape) 264 | 265 | 266 | # ### size 267 | # 268 | 269 | # In[10]: 270 | 271 | 272 | import pandas as pd 273 | import numpy as np 274 | 275 | #Create a Dictionary of series 276 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 277 | 'Age':pd.Series([25,26,25,23,30,29,23]), 278 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 279 | 280 | #Create a DataFrame 281 | df = pd.DataFrame(d) 282 | print ("Our object is:") 283 | print (df) 284 | print ("The total number of elements in our object is:") 285 | print (df.size) 286 | 287 | 288 | # ### values 289 | # 290 | 291 | # In[11]: 292 | 293 | 294 | import pandas as pd 295 | import numpy as np 296 | 297 | #Create a Dictionary of series 298 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 299 | 'Age':pd.Series([25,26,25,23,30,29,23]), 300 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 301 | 302 | #Create a DataFrame 303 | df = pd.DataFrame(d) 304 | print ("Our object is:") 305 | print (df) 306 | print ("The actual data in our data frame is:") 307 | print (df.values) 308 | 309 | 310 | # ### Head & Tail 311 | # 312 | 313 | # In[13]: 314 | 315 | 316 | import pandas as pd 317 | import numpy as np 318 | 319 | #Create a Dictionary of series 320 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 321 | 'Age':pd.Series([25,26,25,23,30,29,23]), 322 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 323 | 324 | #Create a DataFrame 325 | df = pd.DataFrame(d) 326 | print ("Our data frame is:") 327 | print (df) 328 | print ("The first two rows of the data frame is:") 329 | print (df.head(2)) 330 | 331 | 332 | # In[14]: 333 | 334 | 335 | import pandas as pd 336 | import numpy as np 337 | 338 | #Create a Dictionary of series 339 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']), 340 | 'Age':pd.Series([25,26,25,23,30,29,23]), 341 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} 342 | 343 | #Create a DataFrame 344 | df = pd.DataFrame(d) 345 | print ("Our data frame is:") 346 | print (df) 347 | print ("The last two rows of the data frame is:") 348 | print (df.tail(2)) 349 | 350 | 351 | # In[ ]: 352 | 353 | 354 | 355 | 356 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.4-Descriptive Statistics.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | import pandas as pd 8 | import numpy as np 9 | 10 | #Create a Dictionary of series 11 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 12 | 'Lee','David','Gasper','Betina','Andres']), 13 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 14 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 15 | } 16 | 17 | #Create a DataFrame 18 | df = pd.DataFrame(d) 19 | print (df) 20 | 21 | 22 | # ### sum() 23 | 24 | # In[2]: 25 | 26 | 27 | import pandas as pd 28 | import numpy as np 29 | 30 | #Create a Dictionary of series 31 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 32 | 'Lee','David','Gasper','Betina','Andres']), 33 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 34 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 35 | } 36 | 37 | #Create a DataFrame 38 | df = pd.DataFrame(d) 39 | print (df.sum()) 40 | 41 | 42 | # ### axis=1 43 | # 44 | 45 | # In[3]: 46 | 47 | 48 | import pandas as pd 49 | import numpy as np 50 | 51 | #Create a Dictionary of series 52 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 53 | 'Lee','David','Gasper','Betina','Andres']), 54 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 55 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 56 | } 57 | 58 | #Create a DataFrame 59 | df = pd.DataFrame(d) 60 | print (df.sum(1)) 61 | 62 | 63 | # ### mean() 64 | # 65 | 66 | # In[4]: 67 | 68 | 69 | import pandas as pd 70 | import numpy as np 71 | 72 | #Create a Dictionary of series 73 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 74 | 'Lee','David','Gasper','Betina','Andres']), 75 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 76 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 77 | } 78 | 79 | #Create a DataFrame 80 | df = pd.DataFrame(d) 81 | print (df.mean()) 82 | 83 | 84 | # ### std() 85 | # 86 | 87 | # In[5]: 88 | 89 | 90 | import pandas as pd 91 | import numpy as np 92 | 93 | #Create a Dictionary of series 94 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 95 | 'Lee','David','Gasper','Betina','Andres']), 96 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 97 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 98 | } 99 | 100 | #Create a DataFrame 101 | df = pd.DataFrame(d) 102 | print (df.std()) 103 | 104 | 105 | # ### Summarizing Data 106 | # 107 | 108 | # In[6]: 109 | 110 | 111 | import pandas as pd 112 | import numpy as np 113 | 114 | #Create a Dictionary of series 115 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 116 | 'Lee','David','Gasper','Betina','Andres']), 117 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 118 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 119 | } 120 | 121 | #Create a DataFrame 122 | df = pd.DataFrame(d) 123 | print (df.describe()) 124 | 125 | 126 | # In[7]: 127 | 128 | 129 | import pandas as pd 130 | import numpy as np 131 | 132 | #Create a Dictionary of series 133 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 134 | 'Lee','David','Gasper','Betina','Andres']), 135 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 136 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 137 | } 138 | 139 | #Create a DataFrame 140 | df = pd.DataFrame(d) 141 | print (df.describe(include=['object'])) 142 | 143 | 144 | # In[8]: 145 | 146 | 147 | import pandas as pd 148 | import numpy as np 149 | 150 | #Create a Dictionary of series 151 | d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack', 152 | 'Lee','David','Gasper','Betina','Andres']), 153 | 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 154 | 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) 155 | } 156 | 157 | #Create a DataFrame 158 | df = pd.DataFrame(d) 159 | print (df. describe(include='all')) 160 | 161 | 162 | # In[ ]: 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.5-Function Application.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### adder function 5 | 6 | # In[10]: 7 | 8 | 9 | import pandas as pd 10 | import numpy as np 11 | 12 | def adder(ele1,ele2): 13 | return ele1+ele2 14 | 15 | df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) 16 | df.pipe(adder,2) 17 | print (df.pipe(adder,2)) 18 | 19 | 20 | # ### Row or Column Wise Function Application 21 | # 22 | 23 | # ### Example 1 24 | 25 | # In[9]: 26 | 27 | 28 | import pandas as pd 29 | import numpy as np 30 | 31 | df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) 32 | df.apply(np.mean) 33 | print (df.apply(np.mean)) 34 | 35 | 36 | # ### Example 2 37 | # 38 | 39 | # In[11]: 40 | 41 | 42 | import pandas as pd 43 | import numpy as np 44 | 45 | df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) 46 | df.apply(np.mean,axis=1) 47 | print (df.apply(np.mean)) 48 | 49 | 50 | # ### Example 3 51 | # 52 | 53 | # In[12]: 54 | 55 | 56 | import pandas as pd 57 | import numpy as np 58 | 59 | df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) 60 | df.apply(lambda x: x.max() - x.min()) 61 | print (df.apply(np.mean)) 62 | 63 | 64 | # ### Element Wise Function Application 65 | # 66 | 67 | # ### Example 1 68 | 69 | # In[13]: 70 | 71 | 72 | import pandas as pd 73 | import numpy as np 74 | df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) 75 | 76 | # My custom function 77 | df['col1'].map(lambda x:x*100) 78 | print (df.apply(np.mean)) 79 | 80 | 81 | # ### Example 2 82 | 83 | # In[15]: 84 | 85 | 86 | import pandas as pd 87 | import numpy as np 88 | 89 | # My custom function 90 | df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) 91 | df.applymap(lambda x:x*100) 92 | print (df.apply(np.mean)) 93 | 94 | 95 | # In[ ]: 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.6-Reindexing and Iteration.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | import pandas as pd 8 | import numpy as np 9 | 10 | N=20 11 | 12 | df = pd.DataFrame({ 13 | 'A': pd.date_range(start='2016-01-01',periods=N,freq='D'), 14 | 'x': np.linspace(0,stop=N-1,num=N), 15 | 'y': np.random.rand(N), 16 | 'C': np.random.choice(['Low','Medium','High'],N).tolist(), 17 | 'D': np.random.normal(100, 10, size=(N)).tolist() 18 | }) 19 | 20 | #reindex the DataFrame 21 | df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B']) 22 | print (df_reindexed) 23 | 24 | 25 | # ### Reindex to Align with Other Objects 26 | # 27 | 28 | # In[2]: 29 | 30 | 31 | import pandas as pd 32 | import numpy as np 33 | 34 | df1 = pd.DataFrame(np.random.randn(10,3),columns=['col1','col2','col3']) 35 | df2 = pd.DataFrame(np.random.randn(7,3),columns=['col1','col2','col3']) 36 | 37 | df1 = df1.reindex_like(df2) 38 | print (df1) 39 | 40 | 41 | # ### Filling while ReIndexing 42 | # 43 | 44 | # In[4]: 45 | 46 | 47 | import pandas as pd 48 | import numpy as np 49 | 50 | df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3']) 51 | df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3']) 52 | 53 | # Padding NAN's 54 | print (df2.reindex_like(df1)) 55 | 56 | # Now Fill the NAN's with preceding Values 57 | print ("Data Frame with Forward Fill:") 58 | print (df2.reindex_like(df1,method='ffill')) 59 | 60 | 61 | # ### Limits on Filling while Reindexing 62 | # 63 | 64 | # In[5]: 65 | 66 | 67 | import pandas as pd 68 | import numpy as np 69 | 70 | df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3']) 71 | df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3']) 72 | 73 | # Padding NAN's 74 | print (df2.reindex_like(df1)) 75 | 76 | # Now Fill the NAN's with preceding Values 77 | print ("Data Frame with Forward Fill limiting to 1:") 78 | print (df2.reindex_like(df1,method='ffill',limit=1)) 79 | 80 | 81 | # ### Renaming 82 | # 83 | 84 | # In[8]: 85 | 86 | 87 | import pandas as pd 88 | import numpy as np 89 | 90 | df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3']) 91 | print (df1) 92 | 93 | print ("After renaming the rows and columns:") 94 | print (df1.rename(columns={'col1' : 'c1', 'col2' : 'c2'}, 95 | index = {0 : 'apple', 1 : 'banana', 2 : 'durian'})) 96 | 97 | 98 | # ### Iterating a DataFrame 99 | # 100 | 101 | # In[9]: 102 | 103 | 104 | import pandas as pd 105 | import numpy as np 106 | 107 | N=20 108 | df = pd.DataFrame({ 109 | 'A': pd.date_range(start='2016-01-01',periods=N,freq='D'), 110 | 'x': np.linspace(0,stop=N-1,num=N), 111 | 'y': np.random.rand(N), 112 | 'C': np.random.choice(['Low','Medium','High'],N).tolist(), 113 | 'D': np.random.normal(100, 10, size=(N)).tolist() 114 | }) 115 | 116 | for col in df: 117 | print (col) 118 | 119 | 120 | # ### iteritems() 121 | # 122 | 123 | # In[10]: 124 | 125 | 126 | import pandas as pd 127 | import numpy as np 128 | 129 | df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3']) 130 | for key,value in df.iteritems(): 131 | print (key,value) 132 | 133 | 134 | # ### iterrows() 135 | # 136 | 137 | # In[12]: 138 | 139 | 140 | import pandas as pd 141 | import numpy as np 142 | 143 | df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3']) 144 | for row_index,row in df.iterrows(): 145 | print (row_index,row) 146 | 147 | 148 | # ### itertuples() 149 | # 150 | 151 | # In[13]: 152 | 153 | 154 | import pandas as pd 155 | import numpy as np 156 | 157 | df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3']) 158 | for row in df.itertuples(): 159 | print (row) 160 | 161 | 162 | # In[14]: 163 | 164 | 165 | import pandas as pd 166 | import numpy as np 167 | 168 | df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3']) 169 | 170 | for index, row in df.iterrows(): 171 | row['a'] = 10 172 | print (df) 173 | 174 | 175 | # In[ ]: 176 | 177 | 178 | 179 | 180 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.7-Pandas Sorting.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[3]: 5 | 6 | 7 | import pandas as pd 8 | import numpy as np 9 | 10 | unsorted_df=pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7], 11 | columns=['col2','col1']) 12 | 13 | print (unsorted_df) 14 | 15 | 16 | # ### By Label 17 | # 18 | 19 | # In[4]: 20 | 21 | 22 | import pandas as pd 23 | import numpy as np 24 | 25 | unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7], 26 | columns = ['col2','col1']) 27 | 28 | sorted_df=unsorted_df.sort_index() 29 | print (sorted_df) 30 | 31 | 32 | # ### Order of Sorting 33 | # 34 | 35 | # In[5]: 36 | 37 | 38 | import pandas as pd 39 | import numpy as np 40 | 41 | unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7], 42 | columns = ['col2','col1']) 43 | 44 | sorted_df = unsorted_df.sort_index(ascending=False) 45 | print (sorted_df) 46 | 47 | 48 | # ### Sort the Columns 49 | # 50 | 51 | # In[6]: 52 | 53 | 54 | import pandas as pd 55 | import numpy as np 56 | 57 | unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7], 58 | columns = ['col2','col1']) 59 | 60 | sorted_df=unsorted_df.sort_index(axis=1) 61 | 62 | print (sorted_df) 63 | 64 | 65 | # ### By Value 66 | # 67 | 68 | # In[8]: 69 | 70 | 71 | import pandas as pd 72 | import numpy as np 73 | 74 | unsorted_df = pd.DataFrame({'col1':[2,1,1,1],'col2':[1,3,2,4]}) 75 | sorted_df = unsorted_df.sort_values(by='col1') 76 | 77 | print (sorted_df) 78 | 79 | 80 | # In[ ]: 81 | 82 | 83 | import pandas as pd 84 | import numpy as np 85 | 86 | unsorted_df = pd.DataFrame({'col1':[2,1,1,1],'col2':[1,3,2,4]}) 87 | sorted_df = unsorted_df.sort_values(by=['col1','col2']) 88 | 89 | print (sorted_df) 90 | 91 | 92 | # ### Sorting Algorithm 93 | # 94 | 95 | # In[10]: 96 | 97 | 98 | import pandas as pd 99 | import numpy as np 100 | 101 | unsorted_df = pd.DataFrame({'col1':[2,1,1,1],'col2':[1,3,2,4]}) 102 | sorted_df = unsorted_df.sort_values(by='col1' ,kind='mergesort') 103 | 104 | print (sorted_df) 105 | 106 | 107 | # In[ ]: 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.8-Working with Text Data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[2]: 5 | 6 | 7 | import pandas as pd 8 | import numpy as np 9 | 10 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) 11 | 12 | print (s) 13 | 14 | 15 | # ### lower() 16 | 17 | # In[3]: 18 | 19 | 20 | import pandas as pd 21 | import numpy as np 22 | 23 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) 24 | 25 | print (s.str.lower()) 26 | 27 | 28 | # ### upper() 29 | 30 | # In[5]: 31 | 32 | 33 | import pandas as pd 34 | import numpy as np 35 | 36 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) 37 | 38 | print (s.str.upper()) 39 | 40 | 41 | # ### len() 42 | # 43 | 44 | # In[6]: 45 | 46 | 47 | import pandas as pd 48 | import numpy as np 49 | 50 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) 51 | print (s.str.len()) 52 | 53 | 54 | # ### strip() 55 | # 56 | 57 | # In[7]: 58 | 59 | 60 | import pandas as pd 61 | import numpy as np 62 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 63 | print (s) 64 | print ("After Stripping:") 65 | print (s.str.strip()) 66 | 67 | 68 | # ### split(pattern) 69 | # 70 | 71 | # In[8]: 72 | 73 | 74 | import pandas as pd 75 | import numpy as np 76 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 77 | print (s) 78 | print ("Split Pattern:") 79 | print (s.str.split(' ')) 80 | 81 | 82 | # ### cat(sep=pattern) 83 | # 84 | 85 | # In[9]: 86 | 87 | 88 | import pandas as pd 89 | import numpy as np 90 | 91 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 92 | 93 | print (s.str.cat(sep='_')) 94 | 95 | 96 | # ### get_dummies() 97 | # 98 | 99 | # In[10]: 100 | 101 | 102 | import pandas as pd 103 | import numpy as np 104 | 105 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 106 | 107 | print (s.str.get_dummies()) 108 | 109 | 110 | # ### contains () 111 | # 112 | 113 | # In[11]: 114 | 115 | 116 | import pandas as pd 117 | 118 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 119 | 120 | print (s.str.contains(' ')) 121 | 122 | 123 | # ### replace(a,b) 124 | # 125 | 126 | # In[12]: 127 | 128 | 129 | import pandas as pd 130 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 131 | print (s) 132 | print ("After replacing @ with $:") 133 | print (s.str.replace('@','$')) 134 | 135 | 136 | # ### repeat(value) 137 | # 138 | 139 | # In[13]: 140 | 141 | 142 | import pandas as pd 143 | 144 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 145 | 146 | print (s.str.repeat(2)) 147 | 148 | 149 | # ### count(pattern) 150 | # 151 | 152 | # In[14]: 153 | 154 | 155 | import pandas as pd 156 | 157 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 158 | 159 | print ("The number of 'm's in each string:") 160 | print (s.str.count('m')) 161 | 162 | 163 | # ### startswith(pattern) 164 | # 165 | 166 | # In[15]: 167 | 168 | 169 | import pandas as pd 170 | 171 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 172 | 173 | print ("Strings that start with 'T':") 174 | print (s.str. startswith ('T')) 175 | 176 | 177 | # ### endswith(pattern) 178 | # 179 | 180 | # In[16]: 181 | 182 | 183 | import pandas as pd 184 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 185 | print ("Strings that end with 't':") 186 | print (s.str.endswith('t')) 187 | 188 | 189 | # ### find(pattern) 190 | # 191 | 192 | # In[17]: 193 | 194 | 195 | import pandas as pd 196 | 197 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 198 | 199 | print (s.str.find('e')) 200 | 201 | 202 | # ### findall(pattern) 203 | # 204 | 205 | # In[18]: 206 | 207 | 208 | import pandas as pd 209 | 210 | s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 211 | 212 | print (s.str.findall('e')) 213 | 214 | 215 | # ### swapcase() 216 | # 217 | 218 | # In[19]: 219 | 220 | 221 | import pandas as pd 222 | 223 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) 224 | print (s.str.swapcase()) 225 | 226 | 227 | # ### islower() 228 | # 229 | 230 | # In[21]: 231 | 232 | 233 | import pandas as pd 234 | 235 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) 236 | print (s.str.islower()) 237 | 238 | 239 | # ### isupper() 240 | # 241 | 242 | # In[22]: 243 | 244 | 245 | import pandas as pd 246 | 247 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) 248 | 249 | print (s.str.isupper()) 250 | 251 | 252 | # ### isnumeric() 253 | # 254 | 255 | # In[23]: 256 | 257 | 258 | import pandas as pd 259 | 260 | s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) 261 | 262 | print (s.str.isnumeric()) 263 | 264 | 265 | # In[ ]: 266 | 267 | 268 | 269 | 270 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Code_Scripts/3.9-Options and Customization.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## get_option(param) 5 | # 6 | 7 | # ### display.max_rows 8 | # 9 | 10 | # In[3]: 11 | 12 | 13 | import pandas as pd 14 | print (pd.get_option("display.max_rows")) 15 | 16 | 17 | # ### display.max_columns 18 | # 19 | 20 | # In[4]: 21 | 22 | 23 | import pandas as pd 24 | print (pd.get_option("display.max_columns")) 25 | 26 | 27 | # ## set_option(param,value) 28 | # 29 | 30 | # ### display.max_rows 31 | # 32 | 33 | # In[8]: 34 | 35 | 36 | import pandas as pd 37 | 38 | pd.set_option("display.max_rows",80) 39 | 40 | print (pd.get_option("display.max_rows")) 41 | 42 | 43 | # ### display.max_columns 44 | # 45 | 46 | # In[9]: 47 | 48 | 49 | import pandas as pd 50 | 51 | pd.set_option("display.max_columns",30) 52 | 53 | print (pd.get_option("display.max_columns")) 54 | 55 | 56 | # ### reset_option(param) 57 | # 58 | 59 | # In[10]: 60 | 61 | 62 | import pandas as pd 63 | 64 | pd.reset_option("display.max_rows") 65 | print (pd.get_option("display.max_rows")) 66 | 67 | 68 | # ### describe_option(param) 69 | # 70 | 71 | # In[11]: 72 | 73 | 74 | import pandas as pd 75 | pd.describe_option("display.max_rows") 76 | 77 | 78 | # ### option_context() 79 | # 80 | 81 | # In[12]: 82 | 83 | 84 | import pandas as pd 85 | with pd.option_context("display.max_rows",10): 86 | print(pd.get_option("display.max_rows")) 87 | print(pd.get_option("display.max_rows")) 88 | 89 | 90 | # In[ ]: 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/1.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/10.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/2.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/3.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/4.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/5.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/6.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/7.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/8.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.19-Pandas visualization/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.19-Pandas visualization/9.jpg -------------------------------------------------------------------------------- /Volume_II/3-Pandas/Images/3.2-Pandas Dataframe/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sana-Rasheed/Scripts_of_Data_Exploration_and_Analysis_Book/b5b64ebdefdd1c2d1d89aa50bd4a893f3d437831/Volume_II/3-Pandas/Images/3.2-Pandas Dataframe/1.jpg --------------------------------------------------------------------------------