I'm trying to prompt a user to input a column name in a pandas dataframe and then use that input to display information about the column.
the code I've tried:
df = #initializing dataframe
user_input = input('enter column name')
print(df.user_input.describe())
but I got the error:
df has no attribute user_input
assuming that user input is actually valid column name, how can I use the input in such a way?
CodePudding user response:
You can also access a column with df[]
. Try:
df[user_input].describe()
Another way is to use getattr()
:
getattr(df, user_input).describe()
which I think is quite "unnatural".
CodePudding user response:
pandas
lets you lookup a column as an attribute reference if it meets python's syntax rules and doesn't interfere with an existing attribute on the object. In your case, pandas
would look for a column literally named "user_input".
The more general way to lookup a column is with indexing. It does not have the same constraints. So,
f = #initializing dataframe
user_input = input('enter column name')
print(df[user_input].describe())
Now pandas
will use the string entered by the user to look up the column.
One rule[1] of programming is that there should only be one "right way" of doing things. That's obviously not the case for pandas or python in general. But organizations may define what they consider "right". Since attribute lookup of columns only works sometimes, should it be used at all? Debatable!
[1]: The code is more what you'd call 'guidelines' than actual rules. -Hector Barbossa, Captain of the Black Perl.