python – loop through a DataFrame

I have some code that is inside a function. I have renamed some of the variables to make it easier to read.

Essentially, I loop through a DataRame (approximately 40k. Rows).
Every Row of the DataFrame serves as an input function, which performs some operations and returns either a Any or a DataFrame with 3 columns.

Given some different scenarios, I want to add an error message and save a table of all the exceptions of these functions for a later stage.

The code below does what I want it to do, but I feel that this is not the best way.

vinv = []
exc = []

For i, row in df.iterrows ():
try:
# returns a df 3 cols, an unknown number of rows or none
inv = func (Row, x, y)
if inv is None:
Row['Exception'] = & # 39; ERROR1 & # 39;
exc.append (Row)
elif len (inv) == 0:
Row['Exception'] = & # 39; ERROR2 & # 39;
exc.append (Row)
plus:
vinv.append (inv) # append to the table vinv
except exception as e:
Row['Exception'] = e
exc.append (Row) # if there is an error appended to the exceptions table

vinv = pd.concat (vinv, axis = 0)
exc = pd.concat (exc, axis = 1) .T



def func ():
& # 39; & # 39; & Simulated function & # 39; & # 39; & # 39;
df = pd.DataFrame (np.random.rand (np.random.randint (0, 10), 3))

if len (df) == 0:
return none

back df[df.sum(axis=1) < 1]