Apologize if this has been asked before, somehow I am not able to find the answer to this.
Let's say I have two lists of values:
rows = [0,1,2]
cols = [0,2,3]
that represents indexes of rows and columns respectively. The two lists combined signified sort of coordinates in the matrix, i.e (0,0), (1,2), (2,3).
I would like to use those coordinates to change specific cells of the dataframe
without using a loop.
In numpy, this is trivial:
data = np.ones((4,4))
data[rows, cols] = np.nan
array([[nan, 1., 1., 1.],
[ 1., 1., nan, 1.],
[ 1., 1., 1., nan],
[ 1., 1., 1., 1.]])
But in pandas, it seems I am stuck with a loop:
df = pd.DataFrame(np.ones((4,4)))
for _r, _c in zip(rows, cols):
df.iat[_r, _c] = np.nan
Is there a way to use to vectors that lists coordinate-like index to directly modify cells in pandas?
Please note that the answer is not to use iloc instead, this selects the intersection of entire rows and columns.