0

Have a pandas dataframe and some of the cell values are list. Tried to get the cell value out but get some errors.

dd = pd.DataFrame([['apple',[0,2],[3,4],['pear',3,4]],columns = ['product','count1','count2'])

Then this dataframe was saved as a csv file and read back for further processing. Tried to get the cell value out as numbers inside a function

ss = dd[dd['product']=='apple'].copy()
count1 = ss['count1'].values[0]
count2 = ss['count2'].values[0]
if isinstance(count1,list):
   print('it's list')
   count1 = [int(float(i)) for i in count1]
else:
   print('it's not list')
   count1 = int(float(i))

but got errors

ValueError: could not convert string to float: '[0,2]'

here count1 value is '[0,2]' which is string. How could I avoid such error?

newleaf
  • 2,257
  • 8
  • 32
  • 52

0 Answers0