Python – a quick selection

I wrote a Python programme to select rows that contain NULLs. Whenever a null was found, it is written to a separate file.

The programme uses data frames. This allows to treat data as matrix. Once it is interpreted as a matrix, one may inspect each row. If the row contains a NULL, it is written to an external file.

The code:

import pandas as pd

ff = pd.read_csv(‘C:\Users\tmaanen\CloudStation\Pensioenfonds\moeilijk\ff.csv’, delimiter=’;’,header=0)
dfObjNULL = pd.DataFrame()
dfObjnonNULL = pd.DataFrame()

for i in range(ff.shape[0]):
if ff.iloc[i,:].isnull().sum() > 0:
dfObjNULL = dfObjNULL.append(ff.iloc[i,:])
else:
dfObjnonNULL = dfObjnonNULL.append(ff.iloc[i,:])

dfObjNULL.to_csv (r’C:\Users\tmaanen\CloudStation\Pensioenfonds\moeilijk\ffNULL.csv’, index = False, header=True, sep=’;’)
dfObjnonNULL.to_csv (r’C:\Users\tmaanen\CloudStation\Pensioenfonds\moeilijk\ffnonNULL.csv’, index = False, header=True, sep=’;’)

The idea is rather straightforward. First a file is written. The content is stored in a data frame. The data frame is then inspected row by row. If a NULL is detected the row is appended to a data frame that is set up to store the rows with NULLs. At the end, the data frame is written to a CSV file.

Door tom