Understanding ( Apply,Applymap ) for Dataframe & (Apply, Map) for Series#
The main aim of using the Pandas .apply() method is to speed up operations and avoid the use of loops for iterating over your data
[92]:
# import pandas and numpy library
import pandas as pd
import numpy as np
# List of Tuples
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)
]
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'),
index = list('abc'))
display(df)
## Directly printing to see both the outputs for both the axis
# print(df.apply(lambda x : print(x),axis=0))
# print(df.apply(lambda x : print(x),axis=1))
#### Applying
x | y | z | |
---|---|---|---|
a | 1 | 2 | 3 |
b | 4 | 5 | 6 |
c | 7 | 8 | 9 |
[95]:
#### Lets see by applying 'apply' it into a series.
# df['x'] = df['x'].apply(lambda x : x*x)
def new_func(x,m):
return x+m
# The additional arguments can be a scalar, but cannot be series.
df['x'] = df['x'].apply(new_func,args=(2,))
## This is not possible.
# df['x'] = df['x'].apply(new_func,args=(df['y'],))
df
[95]:
x | y | z | |
---|---|---|---|
a | 3 | 2 | 3 |
b | 6 | 5 | 6 |
c | 9 | 8 | 9 |
[76]:
#### Lets see by applying 'applymap' it into a series.
# print(df.applymap(lambda x : x+1))
# Applymap cannot be applied to a series object, but series.map is available!!
# print(df['x'].applymap(lambda x : x+1))
[47]:
axis = 0
if axis == 1 :
# axis = 1 means rows : moving down, ie index = [x,y,z]
sliced_series = df.loc['a']
elif axis == 0:
# axis = 0 means columns : moving right, ie index = [a,b,c]
sliced_series=df.loc[:,'x']
# Accessing
# Index of series
print(f"Series Name => {sliced_series.index}")
# Name of the series
print(f"Series Name => {sliced_series.name}")
list(sliced_series.index)
# Index of the series
# df.loc['a']
# Axis =
# x=df.loc[:,'y']
# x.name
# sliced_series.name
Series Name => Index(['a', 'b', 'c'], dtype='object')
Series Name => x
[47]:
['a', 'b', 'c']
[ ]:
# Objects passed to the function are Series objects whose index is either
# the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).
# TODO : Video Explaining the Meaning of Axis:
# 0 : Rows
# 1 : Columns
# ---------------> Axis = 1
# axis = obj._get_axis_number(axis)
# klass: type[FrameApply]
# if axis == 0:
# klass = FrameRowApply
# elif axis == 1:
# klass = FrameColumnApply
[61]:
# Apply function numpy.square() to
# square the values of two rows
def new_f(l):
return l*l
# Applying function to column y and z
column_transform_df = df.apply(lambda x: new_f(x,x['x']) if x.name in ['y', 'z'] else x,
axis = 1)
## Applying function to full series.
column_transform_df = df.apply(lambda x: new_f(x) if x.name in ['y', 'z'] else x,axis = 0)
row_transform_df = df.apply(lambda x: new_f(x) if x.name in ['b', 'c'] else x,axis = 1)
display(df)
# Output
display(f'Column Transformation, Axis=0')
display(column_transform_df)
display(f'Row Transformation, Axis=1')
display(row_transform_df)
x | y | z | |
---|---|---|---|
a | 1 | 2 | 3 |
b | 4 | 5 | 6 |
c | 7 | 8 | 9 |
'Column Transformation, Axis=0'
x | y | z | |
---|---|---|---|
a | 1 | 4 | 9 |
b | 4 | 25 | 36 |
c | 7 | 64 | 81 |
'Row Transformation, Axis=1'
x | y | z | |
---|---|---|---|
a | 1 | 2 | 3 |
b | 16 | 25 | 36 |
c | 49 | 64 | 81 |
Elementwise Apply function to Dataframe#
Custom column wise function to replace columns with applied function.#
[ ]:
def applyPhoneFilter(df,phone_cols,country_col):
'''
This function will apply the phone filter to the dataframe.
and return the filtered dataframe.
country_col is the country column name.Its important.
'''
phone_cc = ''
for phone_col in phone_cols:
phone_list = []
for index,row in df.iterrows():
try:
_phone = str(PhoneCleanerClass.extractPhone(row[phone_col],phone_cc,str(row[country_col]))[0])
phone_list.append(_phone)
# print(_phone)
except Exception as e:
# print(e)
phone_list.append('nan')
df[phone_col] = phone_list
return df