I have a data frame like below:
import pandas as pd
df = pd.DataFrame({'ID':['M001','M002','M003','M004','M005'],
'X001':[0,0,1,0,0],
'X002':[0,0,1,1,0],
'X003':[0,0,1,0,1],
'X004':[1,0,1,0,0],
'X005':[1,0,1,1,0]})
print(df)
And it looks like this:
ID X001 X002 X003 X004 X005
0 M001 0 0 0 1 1
1 M002 0 0 0 0 0
2 M003 1 1 1 1 1
3 M004 0 1 0 0 1
4 M005 0 0 1 0 0
What I want to do is to copy the value in the ID column into the other columns based where the value is 1 as shown below.
ID X001 X002 X003 X004 X005
0 M001 0 0 0 M001 M001
1 M002 0 0 0 0 0
2 M003 M003 M003 M003 M003 M003
3 M004 0 M004 0 0 M004
4 M005 0 0 M005 0 0
What would be the easiest and fastest way to do so on a ~2000 x ~2000 data frame?