def my_function(input1, input2):
# Function Body
return value
def recode_gender(gender): # Define recode_gender()
if gender == 'Female':
return 0 # Return 0 if gender is 'Female'
elif gender == 'Male':
return 1 # Return 1 if gender is 'Male'
else:
return np.nan # Return np.nan
tips['recode'] = tips['sex'].apply(recode_gender) # Apply the function to the sex column
print(tips.head())
def check_null_or_valid(row_data):
"""Function that takes a row of data,
drops all missing values,
and checks if all remaining values are greater than or equal to 0
"""
no_na = row_data.dropna()
numeric = pd.to_numeric(no_na, errors='coerce')
ge0 = numeric >= 0
return ge0
# Check whether the first column is 'Life expectancy'
assert g1800s.columns[0] == 'Life expectancy'
# Check whether the values in the row are valid
assert g1800s.iloc[:, 1:].apply(check_null_or_valid, axis=1).all().all()
# Check that there is only one instance of each country
assert g1800s['Life expectancy'].value_counts()[0] == 1
tips['total_dollar_replace'] = tips.total_dollar.apply(lambda x: x.replace('$', '')) # Write the lambda function using replace
tips['total_dollar_re'] = tips.total_dollar.apply(lambda x: re.findall('\d+\.\d+', x)[0]) # Write the lambda function using regular expressions
print(tips.head())