0

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.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
Dave_D
  • 1
  • 1

1 Answers1

1

If you use iloc you won't get a vectorized function since you're essentially iterating over indices.

What you should do instead is to use NumPy's own fill_diagonal() method

arr = np.random.randint(1,100, 100).reshape(10, -1)
np.fill_diagonal(arr, 0)
df = pd.DataFrame(arr) 
NotAName
  • 3,821
  • 2
  • 29
  • 44
  • I found the Numpy fill_diagonal method earlier today - good timing. Thanks for the help. Will use it rather than an iterative method. – Dave_D May 23 '23 at 01:45