I have a class object called Bar which I has as attributes a distinct flavor pd.DataFrame. I want to call the column of each DataFrame through a loop. What I get back, however, is the string itself rather than the call result.
How could I fix this? & what concept am I missing here? Thank you.
l=['chocolate', 'vanilla', 'strawberry']
def m(s):
xd='Bar.' s '.columns.str[:5]'
return xd
for s in l:
print (m(s))
which returns:
bar.chocolate.columns.str[:5]
bar.vanilla.columns.str[:5]
bar.strawberry.columns.str[:5]
CodePudding user response:
Creating a string will not access attributes. You need to actually fetch them using getattr() like so:
l=['chocolate', 'vanilla', 'strawberry']
def m(df, attribute):
return getattr(df, attribute).columns.str[:5]
for s in l:
print (m(Bar, s))
