Data Analysis using numpy and pandas: Program: import numpy as np x1 = np.array([[1,2,3],[4,5,6]]) print(x1.shape) x2 = np.array([[1,2,3],[4,5,6]]) x2.shape = (3,2) print(x2.size) x11 = np.zeros((5,2), dtype = np.int) print(x11) x13 = np.ones([2,2], dtype=int) x14 = np.arange(10, 20, 2) x15 = np.arange(10, 20, 0.5) i1 = np.array([[21,12,43],[54,25,6],[27,8,99]]) print(np.amin(i1,1)) #across rows print(np.amin(i1,0)) #across cols i2 = np.array([[20,50,80],[30,60,90],[40,70,100]]) print(np.percentile(i2,100)) print(np.median(i2)) i3 = np.array([[1,2,3],[3,4,5],[4,5,6]]) print(np.mean(i3)) i4 = np.array([1,2,3,4]) print(i4) print(np.average(i4)) i5 = np.array([1,2,3,4]) print(np.var(i5)) print(np.std(i5)) O/P: (2, 3) 6 [[0 0] [0 0] [0 0] [0 0] [0 0]] [12 6 8] [21 8 6] 100.0 60.0 3.6666666666666665 [1 2 3 4] 2.5 1.25 1.118033988749895 import pandas as pd dict1 ={'a':1, 'b':2, 'c':3, 'd':4} # Program to Create Data Frame with two dictionaries dict2 ={'a':5, 'b':6, 'c':7, 'd':8, 'e':9} Data = {'first':dict1, 'second':dict2} a = pd.DataFrame(Data) print(a) s1 = pd.Series([1, 3, 4, 5, 6, 2, 9]) # Define series 1 s2 = pd.Series([1.1, 3.5, 4.7, 5.8, 2.9, 9.3]) # Define series 2 s3 = pd.Series(['a', 'b', 'c', 'd', 'e']) # Define series 3 Data1 ={'first':s1, 'second':s2, 'third':s3} # Define Data dfseries = pd.DataFrame(Data1) # Create DataFrame print(dfseries) d1 =[[2, 3, 4], [5, 6, 7]] # Define 2d array 1 d2 =[[2, 4, 8], [1, 3, 9]] # Define 2d array 2 Data ={'first': d1, 'second': d2} # Define Data df2d = pd.DataFrame(Data) # Create DataFrame print(df2d) O/P: first second a 1.0 5 b 2.0 6 c 3.0 7 d 4.0 8 e NaN 9 first second third 0 1 1.1 a 1 3 3.5 b 2 4 4.7 c 3 5 5.8 d 4 6 2.9 e 5 2 9.3 NaN 6 9 NaN NaN first second 0 [2, 3, 4] [2, 4, 8] 1 [5, 6, 7] [1, 3, 9]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter