Home > database >  How do I properly append a Python list with data from one data frame column based on criteria for va
How do I properly append a Python list with data from one data frame column based on criteria for va

Time:10-06

I have the following code below that is supposed to append a list (result_value) with values times 1000 from one data frame column when values in another column for each row meet criteria:

# result_value
result_value = []
detection_limit_unit = []

for unit in data.UNITS:
    if unit == 'mg/kg':
        detection_limit_unit.append('ug/kg')
    else:
        detection_limit_unit.append(unit)
        
for Result in data.Result:
    for unit in data.UNITS:
        if data.UNITS.any() == 'mg/kg':
            result_value.append(Result*1000)
        else:
            result_value.append(RL)

The upper for loop works properly but the lower one appends result_value with far too many values. How should I edit the code to populate the list with the proper number of values?

The data are as follows:

LABSAMPID Result UNITS
P222264-03 ND ug/L
P222264-01 180 ug/L
P222126-02 ND mg/kg
P222126-02 ND mg/kg
P222126-02 ND mg/kg
P222126-02 ND mg/kg
P222126-02 0.18 mg/kg
P222126-03 ND mg/kg
P222264-03 10 ug/L
P222264-03 150 ug/L
P222126-03 ND mg/kg
P222126-03 ND mg/kg
P222126-03 ND mg/kg
P222126-03 ND mg/kg
P222126-03 ND mg/kg
P222126-03 0.195 mg/kg
P222126-02 ND mg/kg
P222126-02 ND mg/kg

CodePudding user response:

So, with more thinking and working through the code, I made edits which solved the problem:

for Result in data.Result:
    if Result == "ND":
        result_value.append("")
    elif Result != "ND":
        if data.UNITS.any() == 'mg/kg':
            result_value.append(Result*1000)
        else:
            result_value.append(Result)

CodePudding user response:

You should use np.where to do your computations in a vectorised manner:

detection_limit_unit = list(np.where(data.UNITS == 'mg/kg', 'ug/kg', data.UNITS))
# ['ug/L', 'ug/L', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/L', 'ug/L', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg', 'ug/kg']
result_value = list(np.where(data['Result'] == 'ND', 'ND', pd.to_numeric(data['Result'], errors='coerce') * np.where((data['UNITS']=='mg/kg'), 1000, 1)))
# ['ND', '180.0', 'ND', 'ND', 'ND', 'ND', '180.0', 'ND', '10.0', '150.0', 'ND', 'ND', 'ND', 'ND', 'ND', '195.0', 'ND', 'ND']
  • Related