![](/img/trans.png)
[英]How to change the pandas dataframe 'unnamed 0' column datatype from int64 to string or object
[英]How to convert value datatype in pandas column with JSON from big number to int64?
我正在使用 read_gbq 讀取一個嵌套的 Bigquery 表並獲取一些大數字的 json 列表
data = pd.read_gbq(sql, project_id=project)
這是其中一個帶有 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}}]
里面是 'int_value': -2.047602554786245e+18 但它應該是 -2047602554786245165
我試圖將列轉換為字符串
data['events'].astype(str)
然后是 int 然后是 string
data.astype("Int64").astype(str))
但它仍然是一個帶有數組的 object 並且在 t 中修改了大數字
我怎樣才能在這個單元格中得到完整的整數以及如何將它應用到列中?
[{'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}}]
通過進一步調查,我發現這個值是浮動的,並得出這個 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.