import pandas as pd
import matplotlib.pyplot as plt
def load_data(file_path):
"""
Load data from a CSV file.
Parameters:
file_path (str): The path to the CSV file.
Returns:
pd.DataFrame: The loaded data as a pandas DataFrame.
"""
try:
data = pd.read_csv(file_path)
return data
except FileNotFoundError:
print(f"Error: The file at {file_path} was not found.")
return None
except Exception as e:
print(f"Error: {e}")
return None
def process_data(data):
"""
Process the data to calculate the average of a specific column.
Parameters:
data (pd.DataFrame): The input data as a pandas DataFrame.
Returns:
float: The average value of the specified column.
"""
try:
# Assuming the column to be processed is named 'value'
average_value = data['value'].mean()
return average_value
except KeyError:
print("Error: The specified column 'value' was not found in the data.")
return None
except Exception as e:
print(f"Error: {e}")
return None
def plot_data(data):
"""
Plot the data.
Parameters:
data (pd.DataFrame): The input data as a pandas DataFrame.
"""
try:
# Assuming the columns to be plotted are 'x' and 'y'
plt.figure(figsize=(10, 6))
plt.plot(data['x'], data['y'], marker='o', linestyle='-', color='b')
plt.title('Data Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
except KeyError:
print("Error: The specified columns 'x' and 'y' were not found in the data.")
except Exception as e:
print(f"Error: {e}")
def main():
file_path = 'data.csv'
data = load_data(file_path)
if data is not None:
average_value = process_data(data)
if average_value is not None:
print(f"Average value: {average_value}")
plot_data(data)
if __name__ == "__main__":
main()