Introduction to Pandas

3 of 13 · Introducing DataFrames

Learn

On the previous screen, we discovered that our data has 500 rows, 16 columns, and is stored as a pandas.core.frame.DataFrame object — or simply a DataFrame, which is the main pandas data structure.

Remember, one of the key features that makes pandas perfect for working with data is its support for string columns and row labels:

  • Axis values can have string labels, not just numeric ones.
  • DataFrames can contain columns with multiple data types, including: integer, float, string, and datetime.

Viewing Rows from the Top

Now, let's see this in action! To get a look at the first few rows of our DataFrame, we can use the DataFrame.head() method. By default, it returns the first five rows of our DataFrame. However, it also accepts an optional integer argument, which specifies the number of rows to display:

This displays a neat table showing the top 3 companies in the dataset.

rankrevenuesrevenue_changeprofitsassetsprofit_changeceoindustrysectorprevious_rankcountryhq_locationwebsiteyears_on_global_500_listemployeestotal_stockholder_equity
Walmart14858730.813643.0198825-7.2C. Douglas McMillonGeneral MerchandisersRetailing1USABentonville, ARhttp://www.walmart.com23230000077798
State Grid2315199-4.49571.3489838-6.2Kou WeiUtilitiesEnergy2ChinaBeijing, Chinahttp://www.sgcc.com.cn17926067209456
Sinopec Group3267518-9.11257.9310726-65.0Wang YupuPetroleum RefiningEnergy4ChinaBeijing, Chinahttp://www.sinopec.com19713288106523

Viewing Rows from the Bottom

Similarly, we can use the DataFrame.tail() method to reveal the last rows of our DataFrame:

This presents another table, this time showcasing the bottom 3 companies in the dataset.

rankrevenuesrevenue_changeprofitsassetsprofit_changeceoindustrysectorprevious_rankcountryhq_locationwebsiteyears_on_global_500_listemployeestotal_stockholder_equity
Wm. Morrison Supermarkets49821741-11.3406.41163020.4David T. PottsFood and Drug StoresFood & Drug Stores437BritainBradford, Britainhttp://www.morrisons.com13772105111
TUI49921655-5.51151.716247195.5Friedrich JoussenTravel ServicesBusiness Services467GermanyHanover, Germanyhttp://www.tuigroup.com23667793006
AutoNation500216093.6430.510060-2.7Michael J. JacksonSpecialty RetailersRetailing0USAFort Lauderdale, FLhttp://www.autonation.com12260002310

The f500 variable we created on the previous screen is available to you on this screen.

Now let's practice using these methods to explore the top and bottom Fortune 500 companies!

Instructions

  1. Use the DataFrame.head() method to select the first 6 rows. Assign the result to f500_top_6.
  2. Use the DataFrame.tail() method to select the last 8 rows. Assign the result to f500_bottom_8.
  3. Use the variable inspector to view the two DataFrames created above. The variable inspector uses HTML to create a nicely formatted version of the data.
Help Center
Username
FAQs and Guides
Exercise Help
Site Status
Message Us
Share Feedback