<jupyter_start><jupyter_text>Introduction to Pandas  Pandas Series<jupyter_code>import pandas as pd


series = pd.Series(['Dave', 'Cheng', 'Udacity', 42, -3232324])
print (series)
series = pd.Series(['Dave', 'Cheng-Han', 359, 9001], index=['Instructor', 'Curriculum Manager',
                              'Course Number', 'Power Level'])
print (series)<jupyter_output>Instructor                 Dave
Curriculum Manager    Cheng-Han
Course Number               359
Power Level                9001
dtype: object<jupyter_text>Note: the use of the keyword "index" in order to define the row names You can also index using pd.Series<jupyter_code>series = pd.Series(['Dave', 'Cheng-Han', 359, 9001], 
                   index=['Instructor', 'Curriculum Manager', 'Course Number', 'Power Level'])
print(series['Course Number'])
print ""
print(series['Instructor'])
print ""
print(series[['Power Level', 'Curriculum Manager', 'Instructor']])
want_list=['Power Level', 'Curriculum Manager']
print(series[want_list])<jupyter_output>Power Level                9001
Curriculum Manager    Cheng-Han
dtype: object<jupyter_text>Note that want_list is a list of indexs that we want<jupyter_code>cuteness = pd.Series([1, 2, 3, 4, 5], index=['Cockroach', 'Fish', 'Mini Pig',
                                                 'Puppy', 'Kitten'])

print (cuteness>2)
print (cuteness[cuteness>3])<jupyter_output>Puppy     4
Kitten    5
dtype: int64<jupyter_text>Dataframe<jupyter_code>data = {
        'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
        'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions','Lions', 'Lions'],
        'wins': [11, 8, 10, 15, 11, 6, 10, 4],
        'losses': [5, 8, 6, 1, 5, 10, 6, 12]
        }
football = pd.DataFrame(data)
print (football)<jupyter_output>losses     team  wins  year
0       5    Bears    11  2010
1       8    Bears     8  2011
2       6    Bears    10  2012
3       1  Packers    15  2011
4       5  Packers    11  2012
5      10    Lions     6  2010
6       6    Lions    10  2011
7      12    Lions     4  2012<jupyter_text>dtypes<jupyter_code>print (football.dtypes) # describe data type of coloums<jupyter_output>losses     int64
team      object
wins       int64
year       int64
dtype: object<jupyter_text>describe<jupyter_code>print (football.describe()) # Describle some statistical facts about the coloums<jupyter_output>losses       wins         year
count   8.000000   8.000000     8.000000
mean    6.625000   9.375000  2011.125000
std     3.377975   3.377975     0.834523
min     1.000000   4.000000  2010.000000
25%     5.000000   7.500000  2010.750000
50%     6.000000  10.000000  2011.000000
75%     8.500000  11.000000  2012.000000
max    12.000000  15.000000  2012.000000<jupyter_text>head<jupyter_code>print (football.head()) #the first 5 rows of dataframe<jupyter_output>losses     team  wins  year
0       5    Bears    11  2010
1       8    Bears     8  2011
2       6    Bears    10  2012
3       1  Packers    15  2011
4       5  Packers    11  2012<jupyter_text>tail<jupyter_code>print(football.tail())<jupyter_output>losses     team  wins  year
3       1  Packers    15  2011
4       5  Packers    11  2012
5      10    Lions     6  2010
6       6    Lions    10  2011
7      12    Lions     4  2012<jupyter_text>Importing from pandas<jupyter_code>from pandas import DataFrame, Series

people = ['Sarah', 'Mike', 'Chrisna']
ages  =  [28, 32, 25]
df = DataFrame({'name' : Series(people),'age'  : Series(ages)})
print (df)
print ""
df = DataFrame({'name':people, 'ages':ages})
print(df)
print ""
d = {'name': people, 'ages': ages}
df = DataFrame(d)
print df<jupyter_output>age     name
0   28    Sarah
1   32     Mike
2   25  Chrisna

   ages     name
0    28    Sarah
1    32     Mike
2    25  Chrisna

   ages     name
0    28    Sarah
1    32     Mike
2    25  Chrisna<jupyter_text>2014 sochi Winter olympics medal counts<jupyter_code>countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
                 'Netherlands', 'Germany', 'Switzerland', 'Belarus',
                 'Austria', 'France', 'Poland', 'China', 'Korea', 
                 'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
                 'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
                 'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']

gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0]
bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1]

data = { 'counties':Series(countries), 'gold': Series(gold), 'silver': Series(silver), 'bronze':Series(bronze)}

df = DataFrame(data)

print df
print df.dtypes
print(df.head())
print(df.describe())<jupyter_output>bronze       gold     silver
count  26.000000  26.000000  26.000000
mean    3.807692   3.807692   3.730769
std     3.346870   3.826426   3.105578
min     0.000000   0.000000   0.000000
25%     1.000000   1.000000   1.250000
50%     2.000000   2.500000   3.000000
75%     5.750000   5.750000   5.750000
max    12.000000  13.000000  11.000000<jupyter_text>Column in Dataframe<jupyter_code>df['counties']<jupyter_output><empty_output><jupyter_text>You can also reference dataframe coloum this way:<jupyter_code>df.counties<jupyter_output><empty_output><jupyter_text>More than one columns returns a Dataframe<jupyter_code>df[['gold', 'silver','counties']].head()<jupyter_output><empty_output><jupyter_text>Subset our dataframe<jupyter_code>df[df['bronze']>=5.57]
df['counties'][df['bronze']>=5.57]
data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
        'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions','Lions', 'Lions'],
        'wins': [11, 8, 10, 15, 11, 6, 10, 4],
        'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
football = pd.DataFrame(data)
df=DataFrame(football)
print df
df[['team', 'year']]
 df.team
df[:5]<jupyter_output><empty_output><jupyter_text>Row selection via slicing<jupyter_code>df[3:6]<jupyter_output><empty_output><jupyter_text>loc<jupyter_code>df.loc[0]<jupyter_output><empty_output><jupyter_text>Boolean row selection<jupyter_code>df[df.wins>=10]
df.describe() 
df[df.losses>=7]
df
df[(df.wins>=7)&(df.team =="Packers")]
countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
                 'Netherlands', 'Germany', 'Switzerland', 'Belarus',
                 'Austria', 'France', 'Poland', 'China', 'Korea', 
                 'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
                 'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
                 'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']

gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0]
bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1]

olympic_medal_counts = {'country_name':Series(countries),
                        'gold': Series(gold),
                        'silver': Series(silver),
                        'bronze': Series(bronze)}
df = DataFrame(olympic_medal_counts)<jupyter_output><empty_output><jupyter_text>Compute the average number of bronze medals earned by countries who earned at least one gold medal.<jupyter_code>df
df[df.gold>=1]['bronze']
import numpy
df[df.gold>=1]['bronze']
numpy.mean(df[df.gold>=1]['bronze'])<jupyter_output><empty_output><jupyter_text>Create a new Series called avg_medal_count that indicates the average number of gold, silver, and bronze medals earned amongst countries who earned at least one medal of any kind at the 2014 Sochi olympics.<jupyter_code>df
import numpy
df[['bronze', 'gold', 'silver']]

import numpy
df[['bronze', 'gold', 'silver']].apply(numpy.mean)
df[['bronze', 'gold', 'silver']].describe()<jupyter_output><empty_output><jupyter_text>Imagine a point system in which each country is awarded 4 points for eachgold medal,  2 points for each silver medal, and one point for each  bronze medal.<jupyter_code>countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
             'Netherlands', 'Germany', 'Switzerland', 'Belarus',
             'Austria', 'France', 'Poland', 'China', 'Korea', 
             'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
             'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
             'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']

gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0]
bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1]

medals = [gold, silver, bronze ]

scores = [4, 2, 1]

lis=numpy.dot(scores, medals)

points= list(lis)

data = { 'country_name': countries, 'points': points}
df = DataFrame(data)
df<jupyter_output><empty_output><jupyter_text>Read csv into pandas Dataframe<jupyter_code>import pandas as pd
df = pd.read_csv("titanic_data.csv")
df
df.dtypes
import pandas as pd
df=pd.read_csv('../Data/titanic_data.csv')
df<jupyter_output><empty_output><jupyter_text>Apply functionApply applies to Series<jupyter_code>ex= pd.Series([1,2, 3, 4])
def addedThree(num):
    return num+1
ex.apply(addedThree)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1513438566552901, 'fraction_numerical': 0.10697525597269625, 'mean_word_length': 3.2642110050022737, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '371634', 'license': 'Unlicense', 'n_tokens_mistral': 4347, 'n_tokens_neox': 3515, 'n_words': 1008, 'path': 'Pandas/Introduction to Pandas.ipynb', 'repo_name': 'PhillipWongSeven/Machine-Learning-Simplified', 'size': '9376'}