In [1]:
##### This is a tutorial on Pandas under Python 2.7 #####
##### updated 07.28.2017 #####
import sys
print sys.version


2.7.13 |Anaconda custom (64-bit)| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)]

In [2]:
import platform
print platform.python_version()


2.7.13

In [4]:
import pandas as pd
print pd.__version__


0.20.1

In [9]:
## Let's start!
# Q1. How to create a pandas data frame?

#example 1: from Python dict
math_class_result = {}
math_class_result['Name'] = ['aaa', 'bbb', 'ccc']
math_class_result['ID'] = [1, 2, 3]
math_class_result['Score'] = [90, 85, 100]
print math_class_result


{'Score': [90, 85, 100], 'Name': ['aaa', 'bbb', 'ccc'], 'ID': [1, 2, 3]}

In [10]:
# Convert dict to pandas data frame
math_class_result_df = pd.DataFrame.from_dict(math_class_result)
print math_class_result_df


   ID Name  Score
0   1  aaa     90
1   2  bbb     85
2   3  ccc    100

In [11]:
# set column ID as index
math_class_result_df = math_class_result_df.set_index('ID')
print math_class_result_df


   Name  Score
ID            
1   aaa     90
2   bbb     85
3   ccc    100

In [12]:
# add a new column "age"
math_class_result_df['Age'] = 16
print math_class_result_df


   Name  Score  Age
ID                 
1   aaa     90   16
2   bbb     85   16
3   ccc    100   16

In [13]:
# change column "score" to "math score"
math_class_result_df = math_class_result_df.rename(columns={'Score': 'Math score'})
print math_class_result_df


   Name  Math score  Age
ID                      
1   aaa          90   16
2   bbb          85   16
3   ccc         100   16

In [15]:
# sort record by math score
math_class_result_df = math_class_result_df.sort_values(['Math score'], ascending=[1])
print math_class_result_df


   Name  Math score  Age
ID                      
2   bbb          85   16
1   aaa          90   16
3   ccc         100   16

In [18]:
# Let's create a bar plot!
import matplotlib.pyplot as plt
math_class_result_df['Math score'].plot(kind = 'bar')
plt.show()



In [19]:
# don't like the plot style? Let's solve it!
plt.style.use('ggplot')
math_class_result_df['Math score'].plot(kind = 'bar')
plt.show()



In [23]:
# Add some features for our plot
math_class_result_df['Math score'].plot(kind = 'bar')
plt.xlabel('ID')
plt.ylabel('Math score')
plt.title('Math exam result')
plt.xticks(rotation=0)
plt.show()



In [ ]: