I'm having issues with a loop that I want to: a. see if a value in a DF row is greater than a value from a list b. if it is, concatenate the variable name and the value from the list as a string c. if it's not, pass until the loop conditions are met.
This is what I've tried.
import pandas as pd
import numpy as np
df = {'level': ['21', '22', '23', '24', '25', '26', '27', '28', '29', '30']
, 'variable':'age'}
df = pd.DataFrame.from_dict(df)
knots = [0, 25]
df.assign(key = np.nan)
for knot in knots:
if df['key'].items == np.nan:
if df['level'].astype('int') > knot:
df['key'] = df['variable']+"_"+knot.astype('str')
else:
pass
else:
pass
However, this only yields the key column to have NaN values. I'm not sure why it's not placing the concatenation.
NaN/np.nanis special; it's not equal to anything, not even itself. You need to usenp.isnanto test for it. I don't have enough experience to say how this works with pandas dataframes, so this isn't an answer, but the important part here is thatdf['key'].items == np.nanis guaranteed to beFalse.