My datatable is below.
menu_nm | dtl | rcp | |
---|---|---|---|
0 | sandwich | amazing sandwich!!! | bread 10g |
1 | hamburger | bread 20g, vegetable 10g | ??? |
2 | salad | fresh salad!!! | apple sauce 10g, banana 40g, cucumber 5g |
3 | juice | sweet juice!! | orange 50g, water 100ml |
4 | fruits | strawberry 10g, grape 20g, melon 10g | ??? |
and I want to get this datatable
menu_nm | dtl | rcp | |
---|---|---|---|
0 | sandwich | amazing sandwich!!! | bread 10g |
1 | hamburger | bread 20g, vegetable 10g | |
2 | salad | fresh salad!!! | apple sauce 10g, banana 40g, cucumber 5g |
3 | juice | sweet juice!! | orange 50g, water 100ml |
4 | fruits | strawberry 10g, grape 20g, melon 10g |
I want to shift row 1, 4 to rcp column, but I can't find method or logic that I try. I just know that shifting all row and all column, I don't know how I can shift certain row and column.
If you know hint or answer, please tell me. thanks.
CodePudding user response:
assumption: rcp column contains "???" that needs to be replaced with the a values from dtl
# create a filter where value under rcp is "???"
m=df['rcp'].eq('???')
# using loc, shift the values
df.loc[m, 'rcp'] = df['dtl']
df.loc[m, 'dtl'] = ""
df
menu_nm dtl rcp
0 sandwich amazing sandwich!!! bread 10g
1 hamburger bread 20g, vegetable 10g
2 salad fresh salad!!! apple sauce 10g, banana 40g, cucumber 5g
3 juice sweet juice!! orange 50g, water 100ml
4 fruits strawberry 10g, grape 20g, melon 10g
CodePudding user response:
You can access index location using .iloc as below:
>>> df=pd.DataFrame({"COLA":[1,2,3,4], "COLB":[100,200,300,400], "COLC":[1000,2000,3000,4000]})
>>> df
COLA COLB COLC
0 1 100 1000
1 2 200 2000
2 3 300 3000
3 4 400 4000
>>> df['COLC'].iloc[1]=df['COLB'].iloc[1]
>>> df
COLA COLB COLC
0 1 100 1000
1 2 200 200
2 3 300 3000
3 4 400 4000
>>> df['COLB'].iloc[1]=''
>>> df
COLA COLB COLC
0 1 100 1000
1 2 200
2 3 300 3000
3 4 400 4000
Follow similar steps for row 4.