简体   繁体   中英

How to convert value datatype in pandas column with JSON from big number to int64?

I'm reading a nested Bigquery table with read_gbq and getting list of jsons with some big numbers

data = pd.read_gbq(sql, project_id=project)

Here is one of the cells with array with jsons in it

[{'key': 'firebase_screen_id', 'value': {'string_value': None, 'int_value': -2.047602554786245e+18, 'float_value': None, 'double_value': None}},
 {'key': 'ga_session_id', 'value': {'string_value': None, 'int_value': 1620765482.0, 'float_value': None, 'double_value': None}}]

inside is 'int_value': -2.047602554786245e+18 but it should be -2047602554786245165

i tried to convert column to string with

data['events'].astype(str)

and to int then string

data.astype("Int64").astype(str))

but it still an object with array and has modified big number in t

how can i get full int inside this cells and how to apply this to column?

[{'key': 'firebase_screen_id', 'value': {'string_value': None, 'int_value': -2047602554786245165, 'float_value': None, 'double_value': None}},
 {'key': 'ga_session_id', 'value': {'string_value': None, 'int_value': 1620765482.0, 'float_value': None, 'double_value': None}}]

with further investigation i found out that this value was float and come out with this function Not the best use of Exceptions but fine for one time

def values_to_int(json_data):
    result = {}
    for c in json_data:
        value = [e for c, e in c['value'].items() if e or e == 0]
        result[c["key"]] = value
        try:
            if type(result["firebase_screen_id"][0]) == float:
                result["firebase_screen_id"][0] = int(result["firebase_screen_id"][0])
        except Exception:
            continue
    return result

data[col] = data[col].apply(lambda x: values_to_int(x))

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