I have a dataframe like so:
number type X Y Z
1 red 101 NaN 101,NaN
2 blue 101 40,50 101,40,50
3 green 401 70,NaN 101,70,NaN
Is there any way that I can flag each type
element based on the column Z
? Z
is just a combined version of X
and Y
.
So something like:
Number Type Z X_or_Y
1 red 101 X
1 red NaN Y
2 blue 101 X
2 blue 40 Y
2 blue 50 Y
3 green 101 X
3 green 70 Y
3 green NaN Y
I'm thinking of the melt function but not sure how.
reshaped_data = pd.melt(df, id_vars =['number', 'type',
'Z'], value_vars =['X', 'Y'])
**Edit: ** The values in the X_or_Y column should come from the X ans Y columns in the initial dataframe