Looks like no one added any tags here yet for you.
import numpy as np
Import convention for Numpy
np.zeroes(())
Create an array of zeroes
np.ones
Create an array of ones
np.arrange(10,25,5)
Create an array of evenly spaced values (step value_
np.linspace(0,2,9)
Create an array of evenly spaced values
np.full((2,2),7)
Create a constant array
np.eye(2)
Create a 2×2 identity matrix
np.random.random((2,2))
Create an array with random values
np.empty((3,2))
Create an empty array
np.int64
Signed 64-bit integer types
np.float32
Standard double-precision floating point
np.complex
Complex numbers represented by 128 floats
np.bool
Boolean type storing TRUE and FALSE values
np.object
Python object type
np.string_
Fixed-length string type
np.unicode_
Fixed-length unicode type
a.shape
Array dimensions
len(a)
Length of array
b.ndim
Number of array dimensions
e.size
Number of array elements
b.dtype
Data type of array elements
b.dtype.name
Name of data type
b.astype(int)
Convert an array to a different type
np.subtract(a,b)
Subtraction
np.add(b,a)
Addition
np.divide(a,b)
Division
np.multiply(a,b)
Multiplication
np.exp(b)
Exponentiation
np.sqrt(b)
Square root
np.sin(a)
Print sines of an array
np.cos(b)
Element-wise cosine
np.log(a)
Element-wise natural logarithm
e.dot(f)
Dot product
a == b
Element-wise comparison
np.array_equal(a, b)
Array-wise comparison
a.sum()
Array-wise sum
a.min()
Array-wise minimum value
b.max(axis=0)
Maximum value of an array row
b.cumsum(axis=1)
Cumulative sum of the elements
a.mean()
Mean
b.median()
Median
a.corrcoef()
Correlation coefficient
np.std(b)
Standard deviation
h = a.view()
Create a view of the array with the same data
np.copy(a)
Create a copy of the array
h = a.copy()
Create a deep copy of the array
a.sort()
Sort an array
c.sort(axis=0)
Sort the elements of an array's axis
a[2]
Select the element at the 2nd index
b[1,2]
Select the element at row 1 column 2
a[0:2]
Select items at index 0 and 1
b[0:2,1]
Select items at rows 0 and 1 in column 1
b[:1]
Select all items at row 0
a[ : :-1]
Reversed array a
a[a<2]
Select elements from a less than 2
b[[1, 0, 1, 0],[0, 1, 2, 0]]
Select elements (1,0),(0,1),(1,2) and (0,0)
b[[1, 0, 1, 0]][:,[0,1,2,0]]
Select a subset of the matrix’s rows and columns
i = np.transpose(b) or i.T
Permute array dimensions
b.ravel()
Flatten the array
g.reshape(3,-2)
Reshape, but don’t change data
h.resize((2,6))
Return a new array with shape (2,6)
np.append(h,g)
Append items to an array
np.insert(a, 1, 5)
Insert items in an array
np.delete(a,[1])
Delete items from an array
np.concatenate((a,d),axis=0)
Concatenate arrays
np.vstack((a,b))
Stack arrays vertically (row-wise)
np.r_[e,f]
Stack arrays vertically (row-wise)
np.hstack((e,f))
Stack arrays horizontally (column-wise)
np.column_stack((a,d)) or np.c_[a,d]
Create stacked column-wise arrays
np.hsplit(a,3)
Split the array horizontally at the 3rd index
np.vsplit(c,2)
Split the array vertically at the 2nd index
df = pd.DataFrame(
{"a" : [4 ,5, 6],
"b" : [7, 8, 9],
"c" : [10, 11, 12]},
index = [1, 2, 3])
Specify values for each column
df = pd.DataFrame(
[[4, 7, 10],
[5, 8, 11],
[6, 9, 12]],
index=[1, 2, 3],
columns=['a', 'b', 'c'])
Specify values for each row
df = pd.DataFrame(
{"a" : [4 ,5, 6],
"b" : [7, 8, 9],
"c" : [10, 11, 12]},
index = pd.MultiIndex.from_tuples(
[('d',1),('d',2),('e',2)],
names=['n','v'])))
Create DataFrame with a MultiIndex
pd.melt(df)
Gather columns into rows
pd.concat([df1,df2])
Append rows of DataFrames
df.pivot(columns='var', values='val')
Spread rows into columns
pd.concat([df1,df2], axis=1)
Append columns of DataFrames
df[df.Length > 7]
Extract rows that meet logical criteria.
df.drop_duplicates()
Remove duplicate rows (only considers columns)
df.head(n)
Select first n rows.
df.tail(n)
Select last n rows.
df.sample(frac=0.5)
Randomly select fraction of rows.
df.sample(n=10)
Randomly select n rows.
df.iloc[10:20]
Select rows by position
df.nlargest(n, 'value')
Select and order top n entries.
df.nsmallest(n, 'value')
Select and order bottom n entries.
df[['width','length','species']]
Select multiple columns with specific names.
df['width'] or df.width
Select single column with specific name.
df.filter(regex='regex')
Select columns whose name matches regular expression regex
df.loc[:,'x2':'x4']
Select all columns between x2 and x4 (inclusive).
df.iloc[:,[1,2,5]]
Select columns in positions 1, 2 and 5 (first column is 0).
df.loc[df['a'] > 10, ['a','c']]
Select rows meeting logical condition, and only the specific columns .
df['w'].value_counts()
Count number of rows with each unique value of variable
len(df)
# of rows in DataFrame
df['w'].nunique()
df.describe()
sum()
Sum values of each object.