Search code examples
pandasdataframevariablesdatasetassign

How to assign a variable in a column given another variable?


This is my dataset :

Datset example

I want to assign to the different values* such as 'W M I HOLDINGS CORP' and 'MILESTONE SCIENTIFIC INC' another variable in the column 'ticker' in order to have the opportunity to sort them. In the column 'ticker' I need to add WMIH and WLSS respectively to the two different values.

How can I do that ?

I am going to expect the output with something like this :

Output Example


Solution

  • You should be ok with this. Assuming you have your dataset in df variable.

    df.loc[df['comnam'] == 'W M I HOLDINGS CORP', 'ticker'] = 'WMIH'
    df.loc[df['comnam'] == 'MILESTONE SCIENTIFIC INC', 'ticker'] = 'WLSS'