[英]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我正在使用 read_gbq 读取一个嵌套的 Bigquery 表并获取一些大数字的 json 列表
data = pd.read_gbq(sql, project_id=project)
Here is one of the cells with array with jsons in it这是其中一个带有 json 数组的单元格
[{'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里面是 'int_value': -2.047602554786245e+18 但它应该是 -2047602554786245165
i tried to convert column to string with我试图将列转换为字符串
data['events'].astype(str)
and to int then string然后是 int 然后是 string
data.astype("Int64").astype(str))
but it still an object with array and has modified big number in t但它仍然是一个带有数组的 object 并且在 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通过进一步调查,我发现这个值是浮动的,并得出这个 function 不是异常的最佳使用,但一次很好
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.