我需要在现有数据框中创建另一列。对于这一新列,我需要使用for循环传递一些值,并且这些值是字符串类型的。 我无法获得所需的输出。请对此发表您的看法。
我首先创建了一个系列,然后尝试通过for循环将一些字符串值传递给该系列。之后,该系列将通过pandas插入函数传递,以将具有已传递值的系列附加到数据框中。
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)
KeyError Traceback (most recent call last)
<ipython-input-136-bf45be76fb3e> in <module>
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