import re
import pandas as pd

"""
Explanation: Title: Breaking Up A String Into Columns Using Regex In Pandas
Slug: pandas_regex_to_create_columns
Summary: Breaking Up A String Into Columns Using Regex In Pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon  
Based on this tutorial in nbviewer.
Import modules
End of explanation
"""


# Create a dataframe with a single column of strings
data = {'raw': ['Arizona 1 2014-12-23       3242.0',
                'Iowa 1 2010-02-23       3453.7',
                'Oregon 0 2014-06-20       2123.0',
                'Maryland 0 2014-03-14       1123.6',
                'Florida 1 2013-01-15       2134.0',
                'Georgia 0 2012-07-14       2345.6']}
df = pd.DataFrame(data, columns = ['raw'])
df

"""
Explanation: Create a dataframe of raw strings
End of explanation
"""


# Which rows of df['raw'] contain 'xxxx-xx-xx'?
df['raw'].str.contains('....-..-..', regex=True)

"""
Explanation: Search a column of strings for a pattern
End of explanation
"""


# In the column 'raw', extract single digit in the strings
df['female'] = df['raw'].str.extract('(\d)', expand=True)
df['female']

"""
Explanation: Extract the column of single digits
End of explanation
"""


# In the column 'raw', extract xxxx-xx-xx in the strings
df['date'] = df['raw'].str.extract('(....-..-..)', expand=True)
df['date']

"""
Explanation: Extract the column of dates
End of explanation
"""


# In the column 'raw', extract ####.## in the strings
df['score'] = df['raw'].str.extract('(\d\d\d\d\.\d)', expand=True)
df['score']

"""
Explanation: Extract the column of thousands
End of explanation
"""


# In the column 'raw', extract the word in the strings
df['state'] = df['raw'].str.extract('([A-Z]\w{0,})', expand=True)
df['state']

"""
Explanation: Extract the column of words
End of explanation
"""


df

"""
Explanation: View the final dataframe
End of explanation
"""


