1.4 Getting and displaying dataframe columns
You learned in Week 2 that you can get and display a single column of a dataframe by putting the name of the column (in quotes) within square brackets immediately after the dataframe’s name.
For example, like this:
You then get output like this:
Notice that although there is an index, there is no column heading. This is because what is returned is not a new dataframe with a single column but an example of the Series data type.
Each column in a dataframe is an example of a series
The Series data type is a collection of values with an integer index that starts from zero. In addition, the Series data type has many of the same methods and attributes as the DataFrame data type, so you can still execute code like:
Name: TB deaths, dtype: float64
However, pandas does provide a mechanism for you to get and display one or more selected columns as a new dataframe in its own right. To do this you need to use a list. A list in Python consists of one or more items separated by commas and enclosed within square brackets, for example ['Country'] or ['Country', 'Population (1000s)']. This list is then put within outer square brackets immediately after the dataframe’s name, like this:
Note that the column is now named. The expression df[['Country']](with two square brackets) evaluates to a new dataframe (which happens to have a single column) rather than a series.
To get a new dataframe with multiple columns you just need to put more column names in the list, like this:
df[['Country', 'Population (1000s)']].head()
The code has returned a new dataframe with just the 'Country' and 'Population (1000s)’ columns.
Exercise 1 Dataframes and CSV files
Now that you’ve learned about CSV files and more about pandas you are ready to complete Exercise 1 in the exercise notebook 2.
Open the exercise 2 notebook and the data file you used last week WHO POP TB all.csv and save it in the folder you created in Week 1.
If you’re using Anaconda instead of CoCalc, remember that to open the notebook you’ll need to navigate to the notebook using Jupyter. Once it’s open, run the existing code in the notebook before you start the exercise. When you’ve completed the exercise, save the notebook. If you need a quick reminder of how to use Jupyter watch again the video in Week 1 Exercise 1.