# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Exercise 1.02: Create Pandas Series to form a DataFrame
#
# #### 1. Import numpy and pandas package

# +
import pip
try:
    import numpy 
except ImportError:
     pip.main(['install', 'numpy']) 

try:
    import pandas  
except ImportError:
     pip.main(['install', 'pandas']) 
# -

# #### 2. Generate random numbers 

# +
numpy.random.seed(0)
random_number = numpy.random.random(10)

random_number
# -



# #### 3. Set random_number variable to Series and assign it to variable s

s = pandas.Series(random_number)
print(s)

print(type(s))
print(type(random_number))

print(s[2])

# #### 4.  Observe what happens when we include an index

index_value = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
s = pandas.Series(random_number, index=index_value)
print(s)



# ### Exercise 1.2a:  Create set of lists or Series and stack them as a DataFrame
#
# #### 1. Create four Series of the same lengths
#

s1 = pandas.Series([1, 2, 3, 4, 5])
s2 = pandas.Series([6, 7, 8, 9, 10])
s3 = pandas.Series([11, 12, 13, 14, 15])
s4 = pandas.Series([16, 17, 18, 19, 20])

# #### 2. Stack them into DataFrame:

df = pandas.DataFrame({'s1':s1, 's2':s2, 's3':s3, 's4':s4})
df



# ### Exercise 3: Download dataset from World Bank Open data to demonstrate how Pandas read file into a DataFrame
#
# #### 1. Use pandas read_csv() to csv file into variable called temp_aus_df
#
#

temp_aus_df =pandas.read_csv("tas_1991_2016_AUS.csv")

# #### 2. Use Pandas head function to print the first few rows of the DataFrame, temp_aus_df
#

temp_aus_df.head()

# #### 3. Use Pandas tail function to print the last  few rows of the DataFrame, temp_aus_df
#

temp_aus_df.tail()

# #### 4.  What total number of records/rows/observations of the dataframe? 

len(temp_aus_df)

# #### 5. what are the datatypes of the columns in the dataframe?  

temp_aus_df.dtypes

# #### 6. How many missing values in the dataframe?

temp_aus_df.isnull().sum()

# #### 7. Replace missing values in DataFrame

for colname in temp_aus_df.columns:
    if  df[colname].dtype == 'object':
        df[colname] =temp_aus_df[colname].fillna('others')
    else:
        temp_aus_df[colname] = temp_aus_df[colname].fillna(0)

temp_aus_df.to_csv(‘newdata.csv’, index=False)

StarcoderdataJupyterScriptsDedupFiltered