使用dict值,在一列中出现0和字符串的问题



这是我的代码-它将dict中的数据添加到df中,创建了一个新的col。代码使用源df的第一行(仅字符串(。第二列和后面的列由A、B、C、D、E、F和0组成。列是"object",我尝试更改为"string",并用减号替换了0,到目前为止没有成功。

TypeError:序列项0:应为str实例,找到int

def get_dict(c1,c2,c3,c4,c5):
collect = []
result = None
if c1 in dict:
col1 = dict[c1]
else:
col1 = None
if c2 in dict:
col2 = dict[c2]
else:
col2 = None
if c3 in dict:
col3 = dict[c3]
else:
col3 = None
if c4 in dict:
col4 = dict[c4]        
else:
col4 = None
if c5 in dict:
col5 = dict[c5]
else:
col5 = None
collect = [col1,col2,col3,col4,col5]
collection = ','.join(['' if i is None else i for i in collect])

return collection
df1['Perimeter Defense']=df1.apply(lambda k: get_dict(k['1'],k['2'],k['3'],k['4'],k['5'] ),axis=1)
#Incomplete dict
dict= {'Shengelia': 'A',
'Kurbanov': 'F',
'James': 'B',
'Clyburn': 'B',
'Voigtmann': 'C',
'Hilliard': 'B',
'Hackett': 'E',
'Milutinov': '0',
'Bolomboy': '0',
'Strelnieks': 'E',
'Lundberg': 'E',
'Antonov': 'D',
'Ukhov': 'A',
'Eric': '0',
'Khomenko': 'A',
'Pangos': 'A',
'Baron': 'A',
'Thomas': '0'}```
Full error message
TypeError                                 Traceback (most recent call last)
<ipython-input-370-603fced3dd5d> in <module>
27     return collection
28 
---> 29 df1['Perimeter Defense']=df1.apply(lambda k: get_dict(k['1'],k['2'],k['3'],k['4'],k['5'] ),axis=1)
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
7766             kwds=kwds,
7767         )
-> 7768         return op.get_result()
7769 
7770     def applymap(self, func, na_action: Optional[str] = None) -> DataFrame:
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/apply.py in get_result(self)
183             return self.apply_raw()
184 
--> 185         return self.apply_standard()
186 
187     def apply_empty_result(self):
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/apply.py in apply_standard(self)
274 
275     def apply_standard(self):
--> 276         results, res_index = self.apply_series_generator()
277 
278         # wrap results
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/apply.py in apply_series_generator(self)
288             for i, v in enumerate(series_gen):
289                 # ignore SettingWithCopy here in case the user mutates
--> 290                 results[i] = self.f(v)
291                 if isinstance(results[i], ABCSeries):
292                     # If we have a view on v, we need to make a copy because
<ipython-input-370-603fced3dd5d> in <lambda>(k)
27     return collection
28 
---> 29 df1['Perimeter Defense']=df1.apply(lambda k: get_dict(k['1'],k['2'],k['3'],k['4'],k['5'] ),axis=1)
<ipython-input-370-603fced3dd5d> in get_dict(c1, c2, c3, c4, c5)
23         col5 = None
24     collect = [col1,col2,col3,col4,col5]
---> 25     collection = ','.join(['' if i is None else i for i in collect])
26 
27     return collection
TypeError: sequence item 0: expected str instance, int found 

对于使用join,每个数据元素的类型都应该是str,您可以在列表理解中将int转换为字符串,就像这个

collection = ','.join(['' if i is None else str(i) for i in collect])

最新更新