Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I shift columns based on certain row?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you know hint or answer, please tell me. thanks.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading