Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge: Clean and Prepare Price Data | Financial Data Analysis with Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Traders

bookChallenge: Clean and Prepare Price Data

In trading, you often encounter price data with missing values. Properly cleaning and preparing this data is crucial before any analysis or modeling. You will work with a DataFrame containing daily closing prices, some of which are missing. Your task is to identify missing values, fill them using linear interpolation, and then compute basic statistics on the cleaned data. This process ensures your analysis reflects the true nature of the underlying price series, free from gaps that could distort calculations.

123456789101112131415161718192021222324252627282930313233
import pandas as pd # Hardcoded DataFrame of closing prices with missing values data = { "Date": [ "2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-06", "2024-01-07" ], "Close": [ 101.5, None, 102.7, None, 104.2, 105.0, None ] } df = pd.DataFrame(data) df["Date"] = pd.to_datetime(df["Date"]) df.set_index("Date", inplace=True) # Detect missing values print("Missing values per day:") print(df.isnull()) # Fill missing values using linear interpolation df_filled = df.interpolate(method="linear") # Calculate mean and standard deviation of the cleaned price series mean_price = df_filled["Close"].mean() std_price = df_filled["Close"].std() # Print cleaned DataFrame and statistics print("\nCleaned closing prices (after interpolation):") print(df_filled) print("\nMean closing price:", mean_price) print("Standard deviation of closing price:", std_price)
copy
Compito

Swipe to start coding

  • Use the provided DataFrame with missing values.
  • Identify which entries are missing.
  • Fill missing values using linear interpolation.
  • Calculate and print the mean and standard deviation of the cleaned price series.
  • Print the cleaned DataFrame.

Your solution must use pandas. Do not modify the original data structure.

Soluzione

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 7
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

close

bookChallenge: Clean and Prepare Price Data

Scorri per mostrare il menu

In trading, you often encounter price data with missing values. Properly cleaning and preparing this data is crucial before any analysis or modeling. You will work with a DataFrame containing daily closing prices, some of which are missing. Your task is to identify missing values, fill them using linear interpolation, and then compute basic statistics on the cleaned data. This process ensures your analysis reflects the true nature of the underlying price series, free from gaps that could distort calculations.

123456789101112131415161718192021222324252627282930313233
import pandas as pd # Hardcoded DataFrame of closing prices with missing values data = { "Date": [ "2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-06", "2024-01-07" ], "Close": [ 101.5, None, 102.7, None, 104.2, 105.0, None ] } df = pd.DataFrame(data) df["Date"] = pd.to_datetime(df["Date"]) df.set_index("Date", inplace=True) # Detect missing values print("Missing values per day:") print(df.isnull()) # Fill missing values using linear interpolation df_filled = df.interpolate(method="linear") # Calculate mean and standard deviation of the cleaned price series mean_price = df_filled["Close"].mean() std_price = df_filled["Close"].std() # Print cleaned DataFrame and statistics print("\nCleaned closing prices (after interpolation):") print(df_filled) print("\nMean closing price:", mean_price) print("Standard deviation of closing price:", std_price)
copy
Compito

Swipe to start coding

  • Use the provided DataFrame with missing values.
  • Identify which entries are missing.
  • Fill missing values using linear interpolation.
  • Calculate and print the mean and standard deviation of the cleaned price series.
  • Print the cleaned DataFrame.

Your solution must use pandas. Do not modify the original data structure.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 7
single

single

some-alt