I have a column in my dataframe looking like this:
ContextID
7289972
7289972
7289972
7289973
7289973
7304693
7304693
7304693
I am trying to create a new column based on this ContextID
column looking like this:
ContextID Name
7289972 Gas_basics
7289972 Gas_basics
7289972 Gas_basics
7289973 Plasma_basics
7289973 Plasma_basics
7304693 DC2
7304693 DC2
7304693 DC2
I tried the following code
ID = data.ContextID.tolist()
print(ID)
for id in ID:
if (ID == '7289972'):
data['Strategyname'] = 'Plasma_basics'
elif(ID == '7289973'):
data['Strategyname'] = 'Gas_basics'
elif(ID == '7304693'):
data['Strategyname'] = 'DC2'
But it is just creating a variable named id
of type int
and size 1 with value as 7304693.
In the end, I would like to have this newly generated column named as Strategyname
added to the main dataframe named data
.
Can anyone tell me what is the mistake that I am doing so that I can have a better understanding of my mistake and can help me overcome this problem?
Thanks
Edit 1:
I have 2095 unique ContextID
each of them belonging to one of the 3: Gas_basics, Plasma_basics, DC2
Example:
contextid strategyname
7289971 DC2
7289972 Plasma_basics
7289973 Gas_basics
7289997 DC2
7289998 Plasma_basics
7289999 Gas_basics
7289972 Plasma_basics
7289973 Gas_basics
7304693 DC2