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:
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:
print(f500.head(3))
This displays a neat table showing the top 3 companies in the dataset.
rank | revenues | revenue_change | profits | assets | profit_change | ceo | industry | sector | previous_rank | country | hq_location | website | years_on_global_500_list | employees | total_stockholder_equity | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Walmart | 1 | 485873 | 0.8 | 13643.0 | 198825 | -7.2 | C. Douglas McMillon | General Merchandisers | Retailing | 1 | USA | Bentonville, AR | http://www.walmart.com | 23 | 2300000 | 77798 |
State Grid | 2 | 315199 | -4.4 | 9571.3 | 489838 | -6.2 | Kou Wei | Utilities | Energy | 2 | China | Beijing, China | http://www.sgcc.com.cn | 17 | 926067 | 209456 |
Sinopec Group | 3 | 267518 | -9.1 | 1257.9 | 310726 | -65.0 | Wang Yupu | Petroleum Refining | Energy | 4 | China | Beijing, China | http://www.sinopec.com | 19 | 713288 | 106523 |
Similarly, we can use the DataFrame.tail()
method to reveal the last rows of our DataFrame:
xxxxxxxxxx
print(f500.tail(3))
This presents another table, this time showcasing the bottom 3 companies in the dataset.
rank | revenues | revenue_change | profits | assets | profit_change | ceo | industry | sector | previous_rank | country | hq_location | website | years_on_global_500_list | employees | total_stockholder_equity | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Wm. Morrison Supermarkets | 498 | 21741 | -11.3 | 406.4 | 11630 | 20.4 | David T. Potts | Food and Drug Stores | Food & Drug Stores | 437 | Britain | Bradford, Britain | http://www.morrisons.com | 13 | 77210 | 5111 |
TUI | 499 | 21655 | -5.5 | 1151.7 | 16247 | 195.5 | Friedrich Joussen | Travel Services | Business Services | 467 | Germany | Hanover, Germany | http://www.tuigroup.com | 23 | 66779 | 3006 |
AutoNation | 500 | 21609 | 3.6 | 430.5 | 10060 | -2.7 | Michael J. Jackson | Specialty Retailers | Retailing | 0 | USA | Fort Lauderdale, FL | http://www.autonation.com | 12 | 26000 | 2310 |
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!
DataFrame.head()
method to select the first 6 rows. Assign the result to f500_top_6
.DataFrame.tail()
method to select the last 8 rows. Assign the result to f500_bottom_8
.xxxxxxxxxx
Have an account?