'Resetting, fixing the column values using Python Pandas

I'm trying to understand the last 4 lines of this code that reads a data file to a pandas dataframe. I have written a comment above those 4 lines; but I'm still a bit confused. It seems the code is trying to do some cleaning of null values etc. Question: Can someone please explain what exactly the code is doing. It seems it can be simplified a bit more.

import pandas as pd
import numpy as np

pf = pd.read_csv('MyDataFile.txt', low_memory=False, sep=',', encoding='ISO-8859-1')
#trim the column names. Trimmed column will be the new column name
pf = pf.rename(columns=lambda x: x.strip())
#select all the columns that have undefined datatypes
pf_names = pf.select_dtypes(['object'])
#Select all columns with undefined datatypes and trim them
pf[pf_names.columns] = pf_names.apply(lambda x: x.str.strip())
#Replace the null values of dataframe columns with none
pf.replace([np.nan], [None], inplace=True)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source