I have the following two DataFrames:
import pandas as pd
df = pd.DataFrame([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
index = [0, 0.25, 0.50, 0.75, 1],
columns = [0, 0.25, 0.50, 0.75, 1])
df_cross = pd.DataFrame([[0.0, 0.25],
[0.0, 0.75],
[0.5, 1]],
columns = ['indexes_to_keep',
'cols_to_keep'])
df:
0.00 0.25 0.50 0.75 1.00
0.00 0 0 0 0 0
0.25 0 0 0 0 0
0.50 0 0 0 0 0
0.75 0 0 0 0 0
1.00 0 0 0 0 0
df_cross:
indexes_to_keep cols_to_keep
0 0.0 0.25
1 0.0 0.75
2 0.5 1.00
In the df I have my storaged data, and the df_cross contains the indexes and columns that I want to keep the values. The values in df which the index and columns do not match with any row of df_cross I want to replace by a string (for example "NaN").
The expected output is:
0.00 0.25 0.50 0.75 1.00
0.00 NaN 0 NaN 0 NaN
0.25 NaN NaN NaN NaN NaN
0.50 NaN NaN NaN NaN 0
0.75 NaN NaN NaN NaN NaN
1.00 NaN NaN NaN NaN NaN
Thanks in advance.