Parameterized for loop in Python -
i need write parameterized loop.
# works but... df["id"]=np_get_defined(df["methoda"+"id"], df["methodb"+"id"],df["methodc"+"id"]) # need loop follows df["id"]=np_get_defined(df[sm+"id"] sm in strmethods)
and following error:
valueerror: length of values not match length of index
remaining definitions:
import numpy np
df
pandas.dataframe
strmethods=['methoda','methodb','methodc'] def get_defined(*args): strs = [str(arg) arg in args if not pd.isnull(arg) , 'n/a' not in str(arg) , arg!='0'] return ''.join(strs) if strs else none np_get_defined = np.vectorize(get_defined)
df["id"]=np_get_defined(df[sm+"id"] sm in strmethods)
means you're passing generator single argument called method.
if want expand generated sequence list of arguments use *
operator:
df["id"] = np_get_defined(*(df[sm + "id"] sm in strmethods)) # or: df["id"] = np_get_defined(*[df[sm + "id"] sm in strmethods])
the first uses generator , unpacks elements, second uses list comprehension instead, result same in either case.
Comments
Post a Comment