Module 6 NumPY and Pandas
Module 6 NumPY and Pandas
Creating NumPy arrays, Indexing and slicing in NumPy, creating multidimensional arrays,
NumPy Data types, Array Attribute, Indexing and Slicing, Creating array views copies,
Manipulating array shapes I/O
Basics of Pandas, Using multilevel series, Series and Data Frames,
Grouping, aggregating, Merge Data Frames
Numpy
import numpy as np
[ 4, 2, 5]] )
Creating an array
# Python program to demonstrate
b = np.array((1 , 3, 2))
c = np.zeros((3, 4))
e = np.random.random((2, 2))
f = np.arange(0, 30, 5)
g = np.linspace(0, 5, 10)
[5, 2, 4, 2],
[1, 2, 0, 1]])
newarr = arr.reshape(2, 2, 3)
# Flatten array
flarr = arr.flatten()
Array Indexing:
# Python program to demonstrate
import numpy as np
b = np.array((1 , 3, 2))
c = np.zeros((3, 4))
e = np.random.random((2, 2))
f = np.arange(0, 30, 5)
g = np.linspace(0, 5, 10)
[5, 2, 4, 2],
[1, 2, 0, 1]])
newarr = arr.reshape(2, 2, 3)
print ("\nOriginal array:\n", arr)
# Flatten array
flarr = arr.flatten()
Basic operations:
import numpy as np
a = np.array([1, 2, 5, 3])
a *= 2
Unary operators:
# Python program to demonstrate
import numpy as np
[4, 7, 2],
[3, 1, 9]])
arr.max(axis = 1))
arr.min(axis = 0))
arr.sum())
arr.cumsum(axis = 1))
Binary operators:
# Python program to demonstrate
import numpy as np
a = np.array([[1, 2],
[3, 4]])
b = np.array([[4, 3],
[2, 1]])
# add arrays
# matrix multiplication
Universal functions
import numpy as np
# exponential values
a = np.array([0, 1, 2, 3])
Pandas
Creating a series
import pandas as pd
import numpy as np
ser = pd.Series()
print(ser)
# simple array
ser = pd.Series(data)
print(ser)
Pandas Dataframe
import pandas as pd
df = pd.DataFrame()
print(df)
# list of strings
df = pd.DataFrame(lst)
print(df)
Extracting top 5 rows of a dataframe
import pandas as pd
data_top = data.head()
# display
print(data_top)
import pandas as pd
n=9
series=data["Name"]
top = series.head(n = n)
# display
print(top)
# import module
import pandas as pd
df = pd.DataFrame([[9, 4, 8, 9],
[8, 10, 7, 6],
[7, 6, 8, 5]],
columns=['Maths', 'English',
'Science', 'History'])
# display dataset
print(df)
print(df.sum())
#summary of dataset
a = df.groupby('Maths')
print(a.first())
import pandas as pd
df = pd.DataFrame(data1,index=[0, 1, 2, 3])
print('***************')
res1 = pd.concat(frames)
print(res1)
print('************')
Merging of frames
# importing pandas module
import pandas as pd