File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -32,3 +32,28 @@ file_path = "your_file.csv" # Replace with the path to your CSV file
32
32
display_table(file_path)
33
33
34
34
~~~
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
+ ~~~
You can’t perform that action at this time.
0 commit comments