Universal Functions In Pandas
1. UNIVERSAL FUNCTIONS: INDEX PRESERVATION
All NumPy Ufunc will work on Pandas Series and DataFrame
First, letโs create Pandas Series of random integers
import numpy as np
import pandas as pd
# creating random state
rand = np.random.RandomState(42)
# Creating Pandas Series of random integers
ser1 = pd.Series(rand.randint(10, size=4))
print(ser1)0 6
1 3
2 7
3 4
dtype: int64Second, create a Pandas DataFrame of random integers
# Creating Pandas DataFrame
df1 = pd.DataFrame(rand.randint(10,size=(3,4)),
columns=['a','b','c','d'])
print(df1)Now, if we apply any Numpy Ufunc on these objects (Series or DataFrame) the result will be another Panda object with indices preserved
2. UNIVERSAL FUNCTIONS: INDEX ALIGNMENT
2.1. Index Alignment in Series
When we try to add two Series with non-identical index, the resulting sum will keep the index alignment
As we can tell from above example, when we perform the sum, the indices of both series are preserved.
add() method with fill_value
When Python doesnโt find any corresponding value on same index, it returns
NaNFor example, in Series
Athere is index 0 but no corresponding value for SeriesB, index 0To handle this NaN, we can use kwarg
fill_valuewith Pandas.add()method
2.2. Index Alignment in DataFrame
When we try to add two DataFrame with non-identical index, the resulting sum will keep the index alignment
add() method with fill_value
When Python doesnโt find any corresponding value on same index and column, it returns
NaNFor example, in DataFrame
Dthere is index 0, column โcโ but no corresponding value for SeriesCunder index 0, column โcโWe can use keyword argument,
fill_valuewith Pandas.add()method to handle the NaN
2.3. Python Operators and their equivalent Pandas Methods
+
add()
-
sub(),subtract()
*
mul(),multiply()
/
div(),divide(),truediv()
//
floordiv()
%
mod()
**
pow()
3. UNIVERSAL FUNCTIONS: OTHER OPERATIONS
3.1. Understanding โaxisโ keyword argument
One way to look at axis kwarg:
axis kwarg:Remember that we mention, axis=0 or axis=index the operation will be performed column wise and when we mention axis=1 or axis=column, the operation will be performed row wise.
Another way to look at axis kwarg:
axis kwarg:axis=0oraxis=indexmeans to perform operation on all the rows in each columnaxis=1oraxis=columnmeans to perform operation on all the columns in each row
3.2. Operations on Self
Letโs subtract values of first row of the df1 from all rows in df1. In this case, the default value of kwarg, axis is 1 or columns
However, If we would like to apply this arithmetic operation index-wise, we can use, axis=0 or axis=index
3.3. Operation between Series and DataFrame
Operations between a DataFrame and Series object are similar to operations between a two-dimensional and one-dimensional NumPy array
Let add Series to DataFrame with kwarg, axis=0 or axis=index, which matches the index . Both ser1 and df1 have identical index
Last updated
Was this helpful?