如何在某些情况下使用for循环在数据框中插入新列并向其传递值

时间:2019-09-03 10:30:41

标签: pandas dataframe

我需要在现有数据框中创建另一列。对于这一新列,我需要使用for循环传递一些值,并且这些值是字符串类型的。我无法获得所需的输出。

我首先创建了一个系列,然后尝试通过for循环将一些字符串值传递给该系列。之后,该系列将通过pandas插入函数传递,以将具有已传递值的系列附加到数据框中。

在这里我要创建一个名为Type_new的列,其类别为“低电势”和“高电势”。

data2是一个现有的数据框,我正在同一数据框中创建一个名为Type_new的新列

所使用的代码如下所示以及错误消息。

请分享您的观点,以帮助我解决问题。

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)
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

1 个答案:

答案 0 :(得分:0)

因为您的情况只是一个映射,所以您可以这样做:

# Define a dictionary according to your condition to map your status
dictionary = {'Junk Lead': 'Low Potential', 'Not Responding': 'Low Potential',\
              'Just Enquiry': 'Low Potential', 'In Progress Negative': 'Low Potential',\
              'LOST': 'Low Potential', 'CONVERTED': 'High Potential', 'Potential': 'High Potential',\
              'Long Term': 'High Potential', 'In Progress Positive': 'High Potential',\
              'Open': 'High Potential', 'converted': 'High Potential'}

# Store the Status column with the keys replaced by their values from the dictionary
df["Status Clubbed"] = df.Status.replace(dictionary)

文档: