Skip to content

Commit d02c880

Browse files
Update Lab.md
1 parent 25f530d commit d02c880

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Lab_files/Lab.md

+25
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ file_path = "your_file.csv" # Replace with the path to your CSV file
3232
display_table(file_path)
3333

3434
~~~
35+
36+
~~~python
37+
import pandas as pd
38+
39+
# Define a function to calculate the mean of a specified column
40+
def calculate_mean(file_path, column_name):
41+
# Read the CSV file into a DataFrame
42+
df = pd.read_csv(file_path)
43+
44+
# Check if the column exists in the DataFrame
45+
if column_name in df.columns:
46+
# Calculate and return the mean of the specified column
47+
return df[column_name].mean()
48+
else:
49+
# Return a message if the column doesn't exist
50+
return f"Column '{column_name}' not found in the file."
51+
52+
# Example usage
53+
file_path = "your_file.csv" # Replace with the path to your CSV file
54+
column_name = "Specified column" # Replace with your column name
55+
mean_value = calculate_mean(file_path, column_name)
56+
57+
print(mean_value)
58+
59+
~~~

0 commit comments

Comments
 (0)