0

there is data is store in a data frame and i use the fallowing code for result.

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:]))

but there is error show which i unable to remove .

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

how to remove this error. my expecting result look like this table as a simple.

Patient_ID Urea Creatinine Uric Acid SGOT
KYN059AQP 3.0,3 3.0,3 3.0,3 -
KQT767JLU - - - 6.0

The data which store in my data frame like this.

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', '']]"
2
  • 1
    The range function requires an integer as the boundary limit. If you will pass float then it will throw an error. Basically, you need to convert float to int and then use it in range. WHY? Because numbers are continuous in nature if you give float what will be executed < layman explanation. Commented Nov 16, 2022 at 7:42
  • for i in range(len(df['Analyte_line'].fillna(0).astype(int))): ValueError: invalid literal for int() with base 10: "[['Urea', 3.0, '3', ''], ['Creatinine', 3.0, '3', ''], ['Uric Acid', 3.0, '3', '']]" Commented Nov 16, 2022 at 7:55

1 Answer 1

1

Try this:

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

This should convert the float value to int before passing into range().

Sign up to request clarification or add additional context in comments.

6 Comments

for arr in range(int(df['Analyte_line'][i])): ValueError: cannot convert float NaN to integer
I also used .isnull()
then u should use .fillna(0) before accessing the element as my edited answer
both are not working
the syntax of the statement is fine, but u have to cleanse the data before, eg. removing Nan, blank string, non numeric, making sure the index of dataframe is unique
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.