Reader small image

You're reading from  Big Data Analysis with Python

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789955286
Edition1st Edition
Languages
Right arrow
Authors (3):
Ivan Marin
Ivan Marin
author image
Ivan Marin

Ivan Marin is a systems architect and data scientist working at Daitan Group, a Campinas-based software company. He designs big data systems for large volumes of data and implements machine learning pipelines end to end using Python and Spark. He is also an active organizer of data science, machine learning, and Python in So Paulo, and has given Python for data science courses at university level.
Read more about Ivan Marin

Ankit Shukla
Ankit Shukla
author image
Ankit Shukla

Ankit Shukla is a data scientist working with World Wide Technology, a leading US-based technology solution provider, where he develops and deploys machine learning and artificial intelligence solutions to solve business problems and create actual dollar value for clients. He is also part of the company's R&D initiative, which is responsible for producing intellectual property, building capabilities in new areas, and publishing cutting-edge research in corporate white papers. Besides tinkering with AI/ML models, he likes to read and is a big-time foodie.
Read more about Ankit Shukla

Sarang VK
Sarang VK
author image
Sarang VK

Sarang VK is a lead data scientist at StraitsBridge Advisors, where his responsibilities include requirement gathering, solutioning, development, and productization of scalable machine learning, artificial intelligence, and analytical solutions using open source technologies. Alongside this, he supports pre-sales and competency.
Read more about Sarang VK

View More author details
Right arrow

Chapter 01: The Python Data Science Stack


Activity 1: IPython and Jupyter

  1. Open the python_script_student.py file in a text editor, copy the contents to a notebook in IPython, and execute the operations.

  2. Copy and paste the code from the Python script into a Jupyter notebook:

    import numpy as np
    
    def square_plus(x, c):
        return np.power(x, 2) + c
  3. Now, update the values of the x and c variables. Then, change the definition of the function:

    x = 10
    c = 100
    
    result = square_plus(x, c)
    print(result)

    The output is as follows:

    200

Activity 2: Working with Data Problems

  1. Import pandas and NumPy library:

    import pandas as pd
    import numpy as np
  2. Read the RadNet dataset from the U.S. Environmental Protection Agency, available from the Socrata project:

    url = "https://opendata.socrata.com/api/views/cf4r-dfwe/rows.csv?accessType=DOWNLOAD"
    df = pd.read_csv(url)
  3. Create a list with numeric columns for radionuclides in the RadNet dataset:

    columns = df.columns
    id_cols = ['State', 'Location', "Date Posted", 'Date Collected', 'Sample Type', 'Unit']
    columns = list(set(columns) - set(id_cols))
    columns
  4. Use the apply method on one column, with a lambda function that compares the Non-detect string:

    df['Cs-134'] = df['Cs-134'].apply(lambda x: np.nan if x == "Non-detect" else x)
    df.head()

    The output is as follows:

    Figure 1.19: DataFrame after applying the lambda function

  5. Replace the text values with NaN in one column with np.nan:

    df.loc[:, columns] = df.loc[:, columns].applymap(lambda x: np.nan if x == 'Non-detect' else x)
    df.loc[:, columns] = df.loc[:, columns].applymap(lambda x: np.nan if x == 'ND' else x)
  6. Use the same lambda comparison and use the applymap method on several columns at the same time, using the list created in the first step:

    df.loc[:, ['State', 'Location', 'Sample Type', 'Unit']] = df.loc[:, ['State', 'Location',g 'Sample Type', 'Unit']].applymap(lambda x: x.strip())
  7. Create a list of the remaining columns that are not numeric:

    df.dtypes

    The output is as follows:

    Figure 1.20: List of columns and their type

  8. Convert the DataFrame objects into floats using the to_numeric function:

    df['Date Posted'] = pd.to_datetime(df['Date Posted'])
    df['Date Collected'] = pd.to_datetime(df['Date Collected'])
    for col in columns:
        df[col] = pd.to_numeric(df[col])
    df.dtypes

    The output is as follows:

    Figure 1.21: List of columns and their type

  9. Using the selection and filtering methods, verify that the names of the string columns don't have any spaces:

    df['Date Posted'] = pd.to_datetime(df['Date Posted'])
    df['Date Collected'] = pd.to_datetime(df['Date Collected'])
    for col in columns:
        df[col] = pd.to_numeric(df[col])
    df.dtypes

    The output is as follows:

    Figure 1.22: DataFrame after applying the selection and filtering method

Activity 3: Plotting Data with Pandas

  1. Use the RadNet DataFrame that we have been working with.

  2. Fix all the data type problems, as we saw before.

  3. Create a plot with a filter per Location, selecting the city of San Bernardino, and one radionuclide, with the x-axis set to the date and the y-axis with radionuclide I-131:

    df.loc[df.Location == 'San Bernardino'].plot(x='Date Collected', y='I-131')

    The output is as follows:

    Figure 1.23: Plot of Date collected vs I-131

  4. Create a scatter plot with the concentration of two related radionuclides, I-131 and I-132:

    fig, ax = plt.subplots()
    ax.scatter(x=df['I-131'], y=df['I-132'])
    _ = ax.set(
        xlabel='I-131',
        ylabel='I-132',
        title='Comparison between concentrations of I-131 and I-132'
    )

    The output is as follows:

    Figure 1.24: Plot of concentration of I-131 and I-132

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Big Data Analysis with Python
Published in: Apr 2019Publisher: PacktISBN-13: 9781789955286

Authors (3)

author image
Ivan Marin

Ivan Marin is a systems architect and data scientist working at Daitan Group, a Campinas-based software company. He designs big data systems for large volumes of data and implements machine learning pipelines end to end using Python and Spark. He is also an active organizer of data science, machine learning, and Python in So Paulo, and has given Python for data science courses at university level.
Read more about Ivan Marin

author image
Ankit Shukla

Ankit Shukla is a data scientist working with World Wide Technology, a leading US-based technology solution provider, where he develops and deploys machine learning and artificial intelligence solutions to solve business problems and create actual dollar value for clients. He is also part of the company's R&D initiative, which is responsible for producing intellectual property, building capabilities in new areas, and publishing cutting-edge research in corporate white papers. Besides tinkering with AI/ML models, he likes to read and is a big-time foodie.
Read more about Ankit Shukla

author image
Sarang VK

Sarang VK is a lead data scientist at StraitsBridge Advisors, where his responsibilities include requirement gathering, solutioning, development, and productization of scalable machine learning, artificial intelligence, and analytical solutions using open source technologies. Alongside this, he supports pre-sales and competency.
Read more about Sarang VK