I have dataframe with given names of columns and I want to to return a column with specified name:
name_of_column = 'name1' # string variable
I tried to use this:
dataframe.iloc[:, name_of_column]
But it did not work. What should I do?
I have dataframe with given names of columns and I want to to return a column with specified name:
name_of_column = 'name1' # string variable
I tried to use this:
dataframe.iloc[:, name_of_column]
But it did not work. What should I do?
You can just do:
dataframe[column_name]
Will select the column.
iloc()
method finds an item in pandas by index.
More examples the selecting data you can find in Pandas Indexing and Selecting Data
Use loc
instead of iloc
and your syntax will work. iloc
is for indexing by integer position (this is what the i
stands for), while loc
is for indexing by label. So you can use:
dataframe.loc[:, name_of_column]
Having said this, the more usual way to retrieve a series is to use __getitem__
directly:
dataframe[name_of_column]