Choosing Columns
00:00
Choose columns. The pandas read_csv()
and read_excel()
functions have the optional parameter usecols
that you can use to specify the columns you want to load from the file.
00:14 You can pass the list of column names as the corresponding argument. Now you have a DataFrame that contains less data than before. Here, there are only the names of the countries and their areas.
00:35 Instead of the column names, you can also pass their indices.
00:50
You can see the following columns: the column at index 0
contains the row labels, the column at index 1
contains the country names, and the column at index 3
contains the areas.
01:04
Similarly, read_sql()
has the optional parameter columns
that takes a list of column names to read.
01:27
Again, the DataFrame only contains the columns with the names of the countries and their areas. If columns
is None
or omitted, then all of the columns will be read, as you saw before. The default behavior is columns=None
.
Become a Member to join the conversation.