简体   繁体   中英

Converting values in a list of dictionaries from string to float

I have a list of dictionaries, where each item in each dictionary is a string. I am trying to build a formula to pass the whole dataset through and convert all values to a float.

Each dictionary has the following structure:

{'dropoff_datetime': '2014-11-26T22:31:00.000',
 'dropoff_latitude': '40.746769999999998',
 'dropoff_longitude': '-73.997450000000001',
 'fare_amount': '52',
 'imp_surcharge': '0',
 'mta_tax': '0.5',
 'passenger_count': '1',
 'payment_type': 'CSH',
 'pickup_datetime': '2014-11-26T21:59:00.000',
 'pickup_latitude': '40.64499',
 'pickup_longitude': '-73.781149999999997',
 'rate_code': '2',
 'tip_amount': '0',
 'tolls_amount': '5.3300000000000001',
 'total_amount': '57.829999999999998',
 'trip_distance': '18.379999999999999',
 'vendor_id': 'VTS'}

I am trying to cast the values into floats

def float_values(trips):    
    for trip in trips:
        for value in trip:
            trip[value] = float(trip[value])

I am getting error message string indices must be integers

You can iterate over the dictionary using items() or iteritems() depending on your version of python. Some of the values in your dictionary are strings, and therefore, can't be converted. A naive solution is as follows:

def float_values(trips):
    for key, value in trips.items():
        try:
            trips[key] = float(value)
        except ValueError:
            continue

You could also change this to use a comprehension if you so wish, but take care with the isdigit() function

def float_values(trips):
    return {key: float(value) if value.isdigit() else value for key, value in trips.items()}

Add the required code and insert some basic debugging. See this lovely debug blog for help.

def float_values(trips):
    for trip in trips:
        print("trip =", trip)
        for value in trip:
            print("value =", value)
            trip[value] = float(trip[value])

data = {
    'dropoff_datetime': '2014-11-26T22:31:00.000',
    'dropoff_latitude': '40.746769999999998',
    'dropoff_longitude': '-73.997450000000001',
 }

float_values(data)

Output:

trip = dropoff_datetime
value = d
Traceback (most recent call last):
  File "so.py", line 14, in <module>
    float_values(data)
  File "so.py", line 6, in float_values
    trip[value] = float(trip[value])
TypeError: string indices must be integers

You extracted the dict key. Then, instead of using that to index the dict, you decided to iterate through the individual characters of the key itself. Finally, you tried to use the individual letter as an index into the key, which is a simple string.

That's what the Python interpreter saw; it threw you an error message. Instead, delete that innermost loop. trips is the dict, and you use the extracted key to index it:

trips[trip] = float ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM