json python浮点对象不能解释为整数

von4xj4u  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(175)

在 Dataframe 中存储有数据,并且我使用以下代码作为结果。

import pandas as pd
import numpy as np
import json

Data1 = 'Data/lab_202210181540.csv'

############### For Data 1 ####################
data_frame = pd.read_csv(Data1)
data_frame['data'] = data_frame['data'].apply(json.loads)
data_frame = data_frame.rename(columns={'id': 'Sr#'})
data_frame = data_frame.join(pd.json_normalize(data_frame['data']))
select_data = data_frame[['serializer']]
select_data['serializer'] = select_data['serializer'].apply(lambda x: json.loads(x) if pd.notnull(x) else np.nan)
final_select_data = select_data.join(pd.json_normalize(select_data.pop('serializer')))
result = final_select_data['Analyte_line']
df = pd.DataFrame(result)
for i in range(len(df['Analyte_line'])):
    for arr in range(df['Analyte_line'][i]):
        if arr[0] in ['Urea', 'Rapid Malaria']:
            df[arr[0]][i] = "".join(map(lambda x: str(x), arr[1:]))

但有错误显示我无法删除。

for arr in range(df['Analyte_line'][i]):
TypeError: 'float' object cannot be interpreted as an integer

如何消除这个错误。2我期望结果看起来像这个简单的表。
| 患者ID|尿素|肌酸酐|尿酸|谷草转氨酶|
| - -|- -|- -|- -|- -|
| KYN 059 AQP系列|3.0、3| 3.0、3| 3.0、3|- -|
| KQT 767 JLU系列|- -|- -|- -|6.0版本|
这样存储在我的数据框中的数据。

the fallowing data is just an example
Patient_ID,Analyte_line
KYN059AQP,"[['Urea', 3.0, '3', ''], ['Creatinine', 3.0, '3', ''], ['Uric Acid', 3.0, '3', '']]"
    KQT767JLU,"[['Total Protein', '', '6', ''], ['Albumin', '', '6', ''], ['Globulin', '', '4', ''], ['Total Bilirubin', '', '6', ''], ['Direct Bilirubin', '', '4', ''], ['Indirect Bilirubin', '', '4', ''], ['Alkaline Phosphatase', '', '4', ''], ['SGPT', '', '5', ''], ['SGOT', '', '5', ''], ['Gamma GT', '', '5', ''], ['AG Ratio', '', '4', '']]"
PWV009AGQ,"[['HGB', '', '18', ''], ['RBC', '', '1', ''], ['HCT', '', '2', ''], ['MCV', '', '3', ''], ['MCH', '', '3', ''], ['MCHC', '', '3', ''], ['RDWcv', '', '2', ''], ['RDWsd', '', '3', ''], ['WBC', '', '4', ''], ['NEU', '', '5', ''], ['LYM', '', '6', ''], ['MON', '', '', ''], ['BAS', '', '', ''], ['EO', '', '', ''], ['NEU%', '', '', ''], ['LYM%', '', '', ''], ['MON%', '', '', ''], ['EO%', '', '', ''], ['BAS%', '', '', ''], ['PLT', '', '170', ''], ['PCT', '', '3', ''], ['MPV', '', '', ''], ['PDWsd', '', '', ''], ['PDWcv', '', '', ''], ['ESR', '', '5', ''], ['GRA#', '', '', '']]"
PWV009AGQ,"[['Total Protein', '', '23', ''], ['Albumin', '', '2', ''], ['Globulin', '', '2', ''], ['Total Bilirubin', '', '2', ''], ['Direct Bilirubin', 2.0, '', ''], ['Indirect Bilirubin', 2.0, '', ''], ['Alkaline Phosphatase', '', '3', ''], ['SGPT', 1.0, '', ''], ['SGOT', '', '4', ''], ['Gamma GT', 33.0, '31', ''], ['AG Ratio', '', '2', '']]"
dw1jzc5e

dw1jzc5e1#

试试看:

for arr in range(int(df.fillna(0)['Analyte_line'][i])):

这应该会在传入range()之前将float值转换为int。

相关问题