0

I want to be able to copy the values under the allergy column there are three languages. I want use pandas Dataframes to move specific cell values data from the row English to french above it and dutch underneath it. And After it's been copied, I want to delete the row English.

Note: Index is the languages below. They go in the order of French, English, Dutch, French, English, Dutch and so on. the Values of each are the typical responses are given under the column allergy

Index       allergy 

french      ''

english     'MAY CONTAIN: prawn' 

dutch       ''

french      ''

english     'MAY CONTAIN: peanuts'

dutch       ''

french      ''

english     'MAY CONTAIN: milk'

dutch       ''

So again to summarise, copy specific cell values from English to Dutch and French Rows.

edit: I cant any more comments: Yes this is the pattern for this dataset. this ouput is displayed in excel file.

Desired output:

Index        allergy 

french      'MAY CONTAIN: prawn'

english     'MAY CONTAIN: prawn'  # remove

dutch       'MAY CONTAIN: prawn'

french      'MAY CONTAIN: peanuts' 

english     'MAY CONTAIN: peanuts'  # remove

dutch       'MAY CONTAIN: peanuts'

french      'MAY CONTAIN: milk'

english     'MAY CONTAIN: milk'  # remove

dutch       'MAY CONTAIN: milk'

Afterwards, it Should look like this

Final output:

Index        allergy 

french      'MAY CONTAIN: prawn'

dutch       'MAY CONTAIN: prawn'

french      'MAY CONTAIN: peanuts' 

dutch       'MAY CONTAIN: peanuts'

french      'MAY CONTAIN: milk'

dutch       'MAY CONTAIN: milk'
user3821872
  • 143
  • 1
  • 1
  • 10

1 Answers1

2

First filter only english rows and create default index:

df1 = df.loc['english'].reset_index(drop=True)
print (df1)
                  allergy
0    'MAY CONTAIN: prawn'
1  'MAY CONTAIN: peanuts'
2     'MAY CONTAIN: milk'

Then concat twice, sorting and last remove second level:

df = (pd.concat([df1, df1], keys=('french','dutch'))
        .sort_index(level=1)
        .reset_index(level=1, drop=True)
      )
print (df)
                       allergy
french    'MAY CONTAIN: prawn'
dutch     'MAY CONTAIN: prawn'
french  'MAY CONTAIN: peanuts'
dutch   'MAY CONTAIN: peanuts'
french     'MAY CONTAIN: milk'
dutch      'MAY CONTAIN: milk'
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252