Question: Q: 1 ( Answer ) import pandas as pd def import _ data ( csv _ filename ) : Import data from

Q:1(Answer)
import pandas as pd
def import_data(csv_filename):
"""
Import data from a CSV file into a pandas DataFrame.
Parameters:
csv_filename (str): File path for the CSV file.
Returns:
pandas.DataFrame: DataFrame containing the imported data.
"""
# Read the CSV file into a DataFrame
df = pd.read_csv(csv_filename, sep=",", header=0, index_col=None)
return df
# Example usage:
df = import_data("superstore_data.csv")
print(df.head()) # Display the first few rows of the DataFrame
Question: 4(Answer)
import pandas as pd
def compute_profit_per_dollar_sold(df):
# Calculate profit per dollar sold
df['profit_per_dollar_sold']= df['Profit']/ df['Sales']
# Return the dataframe with the new column
return df
# Example of usage:
# Assuming df is your pandas dataframe with columns "Profit" and "Sales"
new_df = compute_profit_per_dollar_sold(df)
Question 5:
Objective: Compute statistics on a variable/column Create a function called compute_mean value that receives as input two arguments: a dataframe and the name of a column. If the column refers to a numeric type (int64 or f loat64), it should return the column average otherwise return a -1. Return the average rounded to two decimals. Hint: E.g., to check if column Profit in the dataframe from question 1 is of type float64, use df[Profit].dtype==float64 Example of usage: mean_value = compute_mean(new_df, "Profit"), where new_df is the dataframe from question 4 should return 28.66.
mean_value = compute_mean(df, "Product Name"), where new_df is the dataframe from question 4 should return -1.
Answer questions 5 separately using the question answers from q1 and Q4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!