Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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
Opgave

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.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Clean and Prepare Price Data

Stryg for at vise menuen

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
Opgave

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 7
single

single

some-alt