DataFrame columns
You can access the list of columns of a DataFrame
with its .columns
attribute.
print(df.columns)
## Index(['Rank', 'Genre', 'Description', 'Director', 'Actors', 'Year',
## 'Runtime (Minutes)', 'Rating', 'Votes', 'Revenue (Millions)',
## 'Metascore'],
## dtype='object')
Columns can be renamed with DataFrame
’s .rename()
method. Let’s say that we think “Runtime (Minutes)” and “Revenue (Millions)” are too long and want to rename these.
df.rename(columns={"Runtime (Minutes)": "Runtime_mins",
"Revenue (Millions)": "Revenue_mils"
}, inplace=True)
print(df.columns)
## Index(['Rank', 'Genre', 'Description', 'Director', 'Actors', 'Year',
## 'Runtime_mins', 'Rating', 'Votes', 'Revenue_mils', 'Metascore'],
## dtype='object')
Accessing DataFrame
columns
You can access a single column as a Series
object by passing the column name to the DataFrame
.
genre_column = df["Genre"]
print(type(genre_column)) ## <class 'pandas.core.series.Series'>
print(genre_column.head())
## Title
## Guardians of the Galaxy Action,Adventure,Sci-Fi
## Prometheus Adventure,Mystery,Sci-Fi
## Split Horror,Thriller
## Sing Animation,Comedy,Family
## Suicide Squad Action,Adventure,Fantasy
## Name: Genre, dtype: object
You can also access one or more columns as a DataFrame
by pasing a list of column names.
genre_column = df[["Genre"]]
print(type(genre_column)) ## <class 'pandas.core.frame.DataFrame'>
columns_df = df[["Genre", "Year", "Runtime_mins"]]
print(columns_df.head(3))
## Genre Year Runtime_mins
## Title
## Guardians of the Galaxy Action,Adventure,Sci-Fi 2014 121
## Prometheus Adventure,Mystery,Sci-Fi 2012 124
## Split Horror,Thriller 2016 117