I have a DataFrame (1000,1000) that has Multi-Index in rows label_y1, label_y2 and columns label_x1, label_x2. I want to iterate through all the rows and columns and set everything to zero except where the selected row and column match. Ideally, this works for a single column and row - both with Multi-Index -, but it could also work for more than one column and row.
The DataFrame looks like:
local or
label_columns1 = ['testing','done']
label_columns2 = ['A', 'B']
label_rows1 = ['testing','done']
label_rows2 = ['A', 'B']
local = pd.DataFrame([[1,2,3,4]], index=pd.MultiIndex.from_product([label_rows1,label_rows2]), columns=pd.MultiIndex.from_product([label_columns1, label_columns2 ]))
print(local)
testing done
A B A B
row1 A 1 2 3 4
B 1 2 3 4
row2 A 1 2 3 4
B 1 2 3 4
In the case of the columns I solved the problem with the following code:
for col in local.columns:
if col != ('done', 'A'):
local[col].values[:] = 0
This yields:
print(local)
testing done
A B A B
row1 A 0 0 3 0
B 0 0 3 0
row2 A 0 0 3 0
B 0 0 3 0
I am doing it similarly for rows. I have also tried with local.iterrrows() and loc the rows but it does not work. Any idea on how can I do this? What I need is this:
print (local)
testing done
A B A B
row1 A 0 0 0 0
B 0 0 0 0
row2 A 0 0 3 0
B 0 0 0 0
local.loc[:,~local.columns.isin([label1])] = 0\\local.loc[~local.index.isin([label2]),:] = 0