How to insert a new column in a dataframe and pass on values to it based on some conditions

I need to create another column in my existing data frame. To this new column, I need to pass on some values using a for loop and the values are of string type. I am unable to obtain the desired output. Kindly share your views on this.

I have first created a series and I tried passing on some string values to this series through a for loop. After this, the series is passed in pandas insert function to append series in the Data Frame with values passed.

Here I want to create a column named Type_new which has the categories as Low Potential and High potential.

data2 is an existing data frame and I am creating this new column called Type_new in the same data frame

The code that I have used is as shown below:

Type_new = pd.Series([])

for i in range(len(data2.Status)):
if data2[‘Status’][i]==‘Junk Lead’:
Type_new[i]=‘Low Potential’
elif data2[‘Status’][i]==‘Not Responding’:
Type_new[i]=‘Low Potential’
elif data2[‘Status’][i]==‘Just Enquiry’:
Type_new[i]=‘Low Potential’
elif data2[‘Status’][i]==‘In Progress Negative’:
Type_new[i]=‘Low Potential’
elif data2[‘Status’][i]==‘LOST’:
Type_new[i]=‘Low Potential’
elif data2[‘Status’][i]==‘CONVERTED’:
Type_new[i]=‘High Potential’
elif data2[‘Status’][i]==‘Potential’:
Type_new[i]=‘High Potential’
elif data2[‘Status’][i]==‘Long Term’:
Type_new[i]=‘High Potential’
elif data2[‘Status’][i]==‘In Progress Positive’:
Type_new[i]=‘High Potential’
elif data2[‘Status’][i]==‘Open’:
Type_new[i]=‘High Potential’
elif data2[‘Status’][i]==‘converted’:
Type_new[i]=‘High Potential’
else:
Type_new[i]= data2[“Status”][i]

data2.insert(6,“Status Clubbed”,Type_new)
data2.head(2)

The error that I get is as shown below:

KeyError Traceback (most recent call last)
in
1 for i in range(len(data2.Status)):
----> 2 if data2[‘Status’][i]==‘Junk Lead’:
3 Type_new[i]=‘Low Potential’
4 elif data2[‘Status’][i]==‘Not Responding’:
5 Type_new[i]=‘Low Potential’

~\Anaconda3\lib\site-packages\pandas\core\series.py in getitem(self, key)
866 key = com.apply_if_callable(key, self)
867 try:
–> 868 result = self.index.get_value(self, key)
869
870 if not is_scalar(result):

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4373 try:
4374 return self._engine.get_value(s, k,
-> 4375 tz=getattr(series.dtype, ‘tz’, None))
4376 except KeyError as e1:
4377 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

Kindly share your views to help me sort out the above issue.

Your question is about the pandas library not about a Jupyter project so I would ask your question in the pandas forum/mailing list instead of here.

Thanks for your reply. Please do let me know if you get any updates on the same.