0% found this document useful (0 votes)
73 views8 pages

Introduction to Pandas Series and DataFrames

1) The document discusses pandas Series and DataFrame data structures. It shows how to create Series from lists, dictionaries, and arrays. It also demonstrates indexing, querying, and modifying Series. 2) DataFrames are introduced as 2D data structures that allow storing and manipulating tabular data. The document shows how to create DataFrames from Series, load data from CSV files, and perform indexing and querying of DataFrames. 3) Examples demonstrate common operations on Series and DataFrames like filtering, aggregating, renaming columns, and changing the index.

Uploaded by

Bhanu Jha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views8 pages

Introduction to Pandas Series and DataFrames

1) The document discusses pandas Series and DataFrame data structures. It shows how to create Series from lists, dictionaries, and arrays. It also demonstrates indexing, querying, and modifying Series. 2) DataFrames are introduced as 2D data structures that allow storing and manipulating tabular data. The document shows how to create DataFrames from Series, load data from CSV files, and perform indexing and querying of DataFrames. 3) Examples demonstrate common operations on Series and DataFrames like filtering, aggregating, renaming columns, and changing the index.

Uploaded by

Bhanu Jha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7/14/2020 Week 2

You are currently looking at version 1.0 of this notebook. To download notebooks and datafiles, as
well as get help on Jupyter notebooks in the Coursera platform, visit the Jupyter Notebook FAQ
([Link] course resource.

The Series Data Structure


In [ ]: import pandas as pd
[Link]?

In [ ]: animals = ['Tiger', 'Bear', 'Moose']


[Link](animals)

In [ ]: numbers = [1, 2, 3]
[Link](numbers)

In [ ]: animals = ['Tiger', 'Bear', None]


[Link](animals)

In [ ]: numbers = [1, 2, None]


[Link](numbers)

In [ ]: import numpy as np
[Link] == None

In [ ]: [Link] == [Link]

In [ ]: [Link]([Link])

In [ ]: sports = {'Archery': 'Bhutan',


'Golf': 'Scotland',
'Sumo': 'Japan',
'Taekwondo': 'South Korea'}
s = [Link](sports)
s

In [ ]: [Link]

In [ ]: s = [Link](['Tiger', 'Bear', 'Moose'], index=['India', 'America', 'Canada'])


s

[Link] [Link] 1/8


7/14/2020 Week 2

In [ ]: sports = {'Archery': 'Bhutan',


'Golf': 'Scotland',
'Sumo': 'Japan',
'Taekwondo': 'South Korea'}
s = [Link](sports, index=['Golf', 'Sumo', 'Hockey'])
s

Querying a Series
In [ ]: sports = {'Archery': 'Bhutan',
'Golf': 'Scotland',
'Sumo': 'Japan',
'Taekwondo': 'South Korea'}
s = [Link](sports)
s

In [ ]: [Link][3]

In [ ]: [Link]['Golf']

In [ ]: s[3]

In [ ]: s['Golf']

In [ ]: sports = {99: 'Bhutan',


100: 'Scotland',
101: 'Japan',
102: 'South Korea'}
s = [Link](sports)

In [ ]: s[0] #This won't call [Link][0] as one might expect, it generates an error instead

In [ ]: s = [Link]([100.00, 120.00, 101.00, 3.00])


s

In [ ]: total = 0
for item in s:
total+=item
print(total)

In [ ]: import numpy as np

total = [Link](s)
print(total)

[Link] [Link] 2/8


7/14/2020 Week 2

In [ ]: #this creates a big series of random numbers


s = [Link]([Link](0,1000,10000))
[Link]()

In [ ]: len(s)

In [ ]: %%timeit -n 100
summary = 0
for item in s:
summary+=item

In [ ]: %%timeit -n 100
summary = [Link](s)

In [ ]: s+=2 #adds two to each item in s using broadcasting


[Link]()

In [ ]: for label, value in [Link]():


s.set_value(label, value+2)
[Link]()

In [ ]: %%timeit -n 10
s = [Link]([Link](0,1000,10000))
for label, value in [Link]():
[Link][label]= value+2

In [ ]: %%timeit -n 10
s = [Link]([Link](0,1000,10000))
s+=2

In [ ]: s = [Link]([1, 2, 3])
[Link]['Animal'] = 'Bears'
s

In [ ]: original_sports = [Link]({'Archery': 'Bhutan',


'Golf': 'Scotland',
'Sumo': 'Japan',
'Taekwondo': 'South Korea'})
cricket_loving_countries = [Link](['Australia',
'Barbados',
'Pakistan',
'England'],
index=['Cricket',
'Cricket',
'Cricket',
'Cricket'])
all_countries = original_sports.append(cricket_loving_countries)

[Link] [Link] 3/8


7/14/2020 Week 2

In [ ]: original_sports

In [ ]: cricket_loving_countries

In [ ]: all_countries

In [ ]: all_countries.loc['Cricket']

The DataFrame Data Structure


In [ ]: import pandas as pd
purchase_1 = [Link]({'Name': 'Chris',
'Item Purchased': 'Dog Food',
'Cost': 22.50})
purchase_2 = [Link]({'Name': 'Kevyn',
'Item Purchased': 'Kitty Litter',
'Cost': 2.50})
purchase_3 = [Link]({'Name': 'Vinod',
'Item Purchased': 'Bird Seed',
'Cost': 5.00})
df = [Link]([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1
[Link]()

In [ ]: [Link]['Store 2']

In [ ]: type([Link]['Store 2'])

In [ ]: [Link]['Store 1']

In [ ]: [Link]['Store 1', 'Cost']

In [ ]: df.T

In [ ]: [Link]['Cost']

In [ ]: df['Cost']

In [ ]: [Link]['Store 1']['Cost']

In [ ]: [Link][:,['Name', 'Cost']]

In [ ]: [Link]('Store 1')

[Link] [Link] 4/8


7/14/2020 Week 2

In [ ]: df

In [ ]: copy_df = [Link]()
copy_df = copy_df.drop('Store 1')
copy_df

In [ ]: copy_df.drop?

In [ ]: del copy_df['Name']
copy_df

In [ ]: df['Location'] = None
df

Dataframe Indexing and Loading


In [ ]: costs = df['Cost']
costs

In [ ]: costs+=2
costs

In [ ]: df

In [ ]: !cat [Link]

In [ ]: df = pd.read_csv('[Link]')
[Link]()

In [ ]: df = pd.read_csv('[Link]', index_col = 0, skiprows=1)


[Link]()

In [ ]: [Link]

[Link] [Link] 5/8


7/14/2020 Week 2

In [ ]: for col in [Link]:


if col[:2]=='01':
[Link](columns={col:'Gold' + col[4:]}, inplace=True)
if col[:2]=='02':
[Link](columns={col:'Silver' + col[4:]}, inplace=True)
if col[:2]=='03':
[Link](columns={col:'Bronze' + col[4:]}, inplace=True)
if col[:1]=='№':
[Link](columns={col:'#' + col[1:]}, inplace=True)

[Link]()

Querying a DataFrame
In [ ]: df['Gold'] > 0

In [ ]: only_gold = [Link](df['Gold'] > 0)


only_gold.head()

In [ ]: only_gold['Gold'].count()

In [ ]: df['Gold'].count()

In [ ]: only_gold = only_gold.dropna()
only_gold.head()

In [ ]: only_gold = df[df['Gold'] > 0]


only_gold.head()

In [ ]: len(df[(df['Gold'] > 0) | (df['Gold.1'] > 0)])

In [ ]: df[(df['Gold.1'] > 0) & (df['Gold'] == 0)]

Indexing Dataframes
In [ ]: [Link]()

In [ ]: df['country'] = [Link]
df = df.set_index('Gold')
[Link]()

In [ ]: df = df.reset_index()
[Link]()

[Link] [Link] 6/8


7/14/2020 Week 2

In [ ]: df = pd.read_csv('[Link]')
[Link]()

In [ ]: df['SUMLEV'].unique()

In [ ]: df=df[df['SUMLEV'] == 50]
[Link]()

In [ ]: columns_to_keep = ['STNAME',
'CTYNAME',
'BIRTHS2010',
'BIRTHS2011',
'BIRTHS2012',
'BIRTHS2013',
'BIRTHS2014',
'BIRTHS2015',
'POPESTIMATE2010',
'POPESTIMATE2011',
'POPESTIMATE2012',
'POPESTIMATE2013',
'POPESTIMATE2014',
'POPESTIMATE2015']
df = df[columns_to_keep]
[Link]()

In [ ]: df = df.set_index(['STNAME', 'CTYNAME'])
[Link]()

In [ ]: [Link]['Michigan', 'Washtenaw County']

In [ ]: [Link][ [('Michigan', 'Washtenaw County'),


('Michigan', 'Wayne County')] ]

Missing values
In [ ]: df = pd.read_csv('[Link]')
df

In [ ]: [Link]?

In [ ]: df = df.set_index('time')
df = df.sort_index()
df

[Link] [Link] 7/8


7/14/2020 Week 2

In [ ]: df = df.reset_index()
df = df.set_index(['time', 'user'])
df

In [ ]: df = [Link](method='ffill')
[Link]()

[Link] [Link] 8/8

You might also like