samedi 29 juin 2019

How to format an If Statements with multiple conditionals inside a function

I'm working on creating a function that will evaluate two conditions from a dataframe and pass a series of prearranged return values given the inputs back to the dataframe should it encounter a NaN. The first condition I'd like to have is a check to see if the value of one column is a NaN (obviously) and then check to another column to see what key the id that has been assigned (1,2,3 etc). The eventual goal is to use the .apply method on the function to fill the NaN values with the values from the function back to the original dataframe or to leave the existing values (if present) alone. What's getting me hung up is that this is the first time I've written anything like this to call within a dataframe and I'm having issue of assignment within the control flow.

This is using python 3.6. I've tried playing around with multiple forms of the below but everything consistently gives me the same type error as it tries to apply the function to the dataframe. This is not the actual dataframe but I made quickly to give you a gist of the issue I'm running into.

Obviously something is off in the function but the result would've ideally updated the NaN value with the 40 value

So far I've tried amending the function in all the ways I can think to make sense to get it to be able to iterate over the dataframe.

import pandas as pd
import numpy as np

frame = {'key' : [1,2,3,4,5],
    'height' : [70, 68, 74, 67, 72],
    'age' : [29,45,'N/A',51,34]}

frame = pd.DataFrame(frame)

frame.replace('N/A',np.nan)

def age (x):
    if (x['age'].isnull()) & (x['key'] == 3):
        return x.replace(np.nan, 40)
    else: 
        return x

result = frame.apply(age) 

Here's a snapshot of the dataframe that I would like to amend

Aucun commentaire:

Enregistrer un commentaire