1

How to tell pandas to ignore incorrect dictionary items in list?

For case of simplicity, if I have case from 1st version solution from previous question:

L =[['Manufacturer: Hyundai',
  'Model: Tucson',
  'Mileage: 258000 km',
  'Registered: 07/2019'],
 ['Manufacturer: Mazda',
  'Model: 6',
  'Year: 2014',
  'Registered: 07/2019',
  'Comfort',
  'Safety']]

df = pd.DataFrame([dict(y.split(':') for y in x) for x in L])
print (df)

Second dict item have 2 last items missing values ('Comfort' and 'Safety') but they are also missing ":" therefore pandas is throwing :

ValueError: dictionary update sequence element #5 has length 1; 2 is required

How to tell pandas to ignore these type of errors and proceed with parsing of list?

cs95
  • 379,657
  • 97
  • 704
  • 746
Hrvoje
  • 13,566
  • 7
  • 90
  • 104
  • Sorry, I thought I read somewhere that it's desirable to link other question if it's related. Thank you both for great answers @jezrael – Hrvoje Dec 05 '18 at 12:07

2 Answers2

1

Just add a little if condition.

pd.DataFrame([
    dict(y.split(':') for y in x if ':' in y) for x in L])

  Manufacturer     Mileage    Model Registered   Year
0      Hyundai   258000 km   Tucson    07/2019    NaN
1        Mazda         NaN        6    07/2019   2014

If you want to include those values as NaN, then change the if to an if-else inside the comprehension.

pd.DataFrame([
    dict(y.split(':') if ':' in y else (y, np.nan) for y in x) for x in L])


   Comfort Manufacturer     Mileage    Model Registered  Safety   Year
0      NaN      Hyundai   258000 km   Tucson    07/2019     NaN    NaN
1      NaN        Mazda         NaN        6    07/2019     NaN   2014
cs95
  • 379,657
  • 97
  • 704
  • 746
1

If values with no : are keys is possible add if-else:

df = pd.DataFrame([dict(y.split(':') if ':' in y else (y, np.nan) for y in x) for x in L])
print (df)
       Comfort Manufacturer     Mileage    Model Registered  Safety   Year
0      NaN      Hyundai   258000 km   Tucson    07/2019     NaN    NaN
1      NaN        Mazda         NaN        6    07/2019     NaN   2014
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Hmm, the `if-else` is only necessary for this example, since "comfort" and "safety" aren't actually in any of the rows. – cs95 Dec 05 '18 at 11:56
  • @coldspeed - yes, agree, not 100% sure if understand if is necessary add value or removing – jezrael Dec 05 '18 at 11:58