-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_csv_2.py
66 lines (55 loc) · 2.32 KB
/
test_csv_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pandas as pd
import os
def analyze_csv(input_file, output_file=None):
# Load CSV file
try:
df = pd.read_csv(input_file)
print(f"File '{input_file}' loaded successfully!")
except Exception as e:
print(f"Error loading file: {e}")
return
# Show the first few rows of the DataFrame
print("\n--- Preview of the data (first 5 rows) ---\n")
print(df.head(), "\n") # Show only the first 5 rows
# Show basic information
print("\n--- Basic Information ---\n")
print(df.info(), "\n")
# Show summary statistics for numeric columns
print("\n--- Summary Statistics ---\n")
print(df.describe(), "\n")
# Show unique value counts for each column
print("\n--- Unique Value Counts for Each Column ---\n")
for col in df.columns:
print(f"\nUnique values in column '{col}':")
print(df[col].value_counts())
# Handle missing data
missing_data = df.isnull().sum()
print("\n--- Missing Data ---\n")
print(missing_data)
# Optionally, clean the data (drop rows with missing values)
clean_option = input("\nDo you want to drop rows with missing values? (yes/no): ").lower()
if clean_option == 'yes':
df_cleaned = df.dropna()
print("\n--- Data after dropping missing values ---\n")
print(df_cleaned.info(), "\n")
else:
df_cleaned = df
# Save cleaned data to a new CSV file if an output path is provided
if output_file:
try:
df_cleaned.to_csv(output_file, index=False)
print(f"\nCleaned data saved to '{output_file}'")
except Exception as e:
print(f"Error saving file: {e}")
if __name__ == "__main__":
# Get input file path from the user
input_file = input("Enter the path of the CSV file you want to analyze: ")
# Check if the file exists
if not os.path.exists(input_file):
print(f"Error: The file '{input_file}' does not exist.")
else:
# Optionally, ask for an output file path
output_file = input("Enter the path to save the cleaned CSV file (or press Enter to skip saving): ")
output_file = output_file if output_file.strip() != '' else None
# Call the function to analyze the CSV
analyze_csv(input_file, output_file)