Want to replace a Python 'for' loop with a vectorized comprehension statement to replace the left diagonal in a pandas DataFrame
with zeroes.
The dataframe
is instantiated using a standard numpy.random.randint()
method.
df = pd.DataFrame(np.random.randint(1,100, 100).reshape(10, -1))
I tried to apply a lambda function that locates/replaces the diagonal cells using the df.iloc[]
method. Was expecting the left diagonal to be replaced with zeroes.
df = df.apply(lambda i : [df.iloc[i, i] * 0 for i in range(df.shape[0])])
The result yields an entire DataFrame
loaded with zeroes. Need help on how to write the lambda function since an assignment is not allowed in the lambda function.