Reputation: 48
this is my code- it adds data from dict to a df, creating a new col. Code was working with first row of source df ( only string). Second and following cols consist of A,B,C,D,E,F and 0. Column is 'object', I tried to change to 'string' and replaced 0 with a minus, so far no success.
TypeError: sequence item 0: expected str instance, int found
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
Upvotes: 0
Views: 47
Reputation: 825
For using join
, each data element should be of type str
, you can cast your int
into string in the list comprehension, like this
collection = ','.join(['' if i is None else str(i) for i in collect])
Upvotes: 1