import pandas as pd
import numpy as np

"""
Explanation: Title: Loading A CSV Into Pandas
Slug: pandas_dataframe_importing_csv
Summary: Loading A CSV Into Pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon  
import modules
End of explanation
"""


raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'last_name': ['Miller', 'Jacobson', ".", 'Milner', 'Cooze'], 
        'age': [42, 52, 36, 24, 73], 
        'preTestScore': [4, 24, 31, ".", "."],
        'postTestScore': ["25,000", "94,000", 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
df

"""
Explanation: Create dataframe (that we will be importing)
End of explanation
"""


df.to_csv('pandas_dataframe_importing_csv/example.csv')

"""
Explanation: Save dataframe as csv in the working director
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv')
df

"""
Explanation: Load a csv
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', header=None)
df

"""
Explanation: Load a csv with no headers
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score', 'Post-Test Score'])
df

"""
Explanation: Load a csv while specifying column names
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', index_col='UID', names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score', 'Post-Test Score'])
df

"""
Explanation: Load a csv with setting the index column to UID
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', index_col=['First Name', 'Last Name'], names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score', 'Post-Test Score'])
df

"""
Explanation: Load a csv while setting the index columns to First Name and Last Name
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', na_values=['.'])
pd.isnull(df)

"""
Explanation: Load a csv while specifying "." as missing values
End of explanation
"""


sentinels = {'Last Name': ['.', 'NA'], 'Pre-Test Score': ['.']}

df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', na_values=sentinels)
df

"""
Explanation: Load a csv while specifying "." and "NA" as missing values in the Last Name column and "." as missing values in Pre-Test Score column
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', na_values=sentinels, skiprows=3)
df

"""
Explanation: Load a csv while skipping the top 3 rows
End of explanation
"""


df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', thousands=',')
df

"""
Explanation: Load a csv while interpreting "," in strings around numbers as thousands seperators
End of explanation
"""


