#!/usr/bin/env python # coding: utf-8 # # Module 3 # # ## Video 14: Working with DataFrames # **Python for the Energy Industry** # # There are a number of built-in functions for exploring the data in a DataFrame. We'll be working with a larger example DataFrame: # In[1]: import pandas as pd import numpy as np OPEC_df = pd.DataFrame({ 'Country': ['Algeria','Angola','Equatorial Guinea','Gabon','Iran','Iraq','Kuwait','Libya','Nigeria','Republic of the Congo','Saudi Arabia','UAE','Venezuela'], 'Region': ['North Africa','Southern Africa','Central Africa','Central Africa','Middle East','Middle East','Middle East','North Africa','West Africa','Central Africa','Middle East','Middle East','South America'], 'Population': [42228408,30809787,1308975,2119275,81800188,38433600,4137312,6678559,195874685,5125821,33702756,9630959,28887118], 'Oil Production': [1348361,1769615,np.nan,210820,3990956,4451516,2923825,384686,1999885,260000,10460710,3106077,2276967], 'Proven Reserves': [12.2e9,8.423e9,np.nan,2e9,157.53e9,143.069e9,101.5e9,48.363e9,37.07e9,1.6e9,266.578e9,97.8e9,299.953e9] }) OPEC_df # For large DataFrames, rather than printing out large amounts of data, we can take a peek at some data with the 'head' and 'tail' functions: # In[2]: OPEC_df.head(3) # In[3]: OPEC_df.tail(3) # We can also take a look at what columns we have, and what type of data they store, with the 'info' function. # In[4]: OPEC_df.info() # We can also get a statistical description of our data. The 'describe' function will return basic stats on all columns with numeric data. There are also functions for each individual statistic. # In[5]: OPEC_df.describe() # In[6]: # get the mean of a single column OPEC_df['Oil Production'].mean() # ### Reading & Writing to File # # Pandas can also be used to write a DataFrame into an excel/csv format, or read in a DataFrame from a file. # In[7]: # write OPEC_df to an excel spreadsheet OPEC_df.to_csv('OPEC_df.csv', index=False) # In[10]: # create a new DataFrame from OPEC_df.csv OPEC_df_copy = pd.read_csv('OPEC_df.csv') OPEC_df_copy # ### Exercise # # Obtain a spreadsheet with some data you would like to explore. You can use the example 'countries.csv' on the course page. # # Make sure the spreadsheet is in the sample folder as this notebook. Read the file in as a DataFrame, and use the above functions to explore the data. # In[ ]: