Learn to code for data analysis
Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

Free course

Learn to code for data analysis

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:

In []:

df['TB deaths']

You then get output like this:

Out[]:

0 13000.00

1 20.00

2 5100.00

3 0.26

4 6900.00

5 1.20

6 570.00

...

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:

In []:

df['TB deaths'].head()

Out[]:

0 13000.00

1 20.00

2 5100.00

3 0.26

4 6900.00

Name: TB deaths, dtype: float64

And

In []:

df['TB deaths'].iloc[2]

Out[]:

5100.00

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:

In []:

df[['Country']].head()

Out[]:

 Country
0Afghanistan
1Albania
2Algeria
3Andorra
4Angola

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:

In []:

df[['Country', 'Population (1000s)']].head()

Out[]:

 CountryPopulation (1000s)
0Afghanistan30552
1Albania3173
2Algeria39208
3Andorra79
4Angola21472

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 [Tip: hold Ctrl and click a link to open it in a new tab. (Hide tip)] .

LCDAB_1

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371