-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_explorer.py
More file actions
50 lines (39 loc) · 1.76 KB
/
Copy pathcsv_explorer.py
File metadata and controls
50 lines (39 loc) · 1.76 KB
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
import pandas as pd
# Step 1: Ask user for CSV file path
file_path = input("Enter the path to your CSV file: ")
try:
# Step 2: Load CSV
data = pd.read_csv(file_path)
# Step 3: Show basic info
print(f"\n✅ File loaded successfully!")
print(f"📊 Number of rows: {data.shape[0]}")
print(f"📊 Number of columns: {data.shape[1]}\n")
# Step 4: Show top 5 rows
print("🔍 Top 5 rows:")
print(data.head())
# Step 5: Ask user if they want to filter
filter_choice = input("\nDo you want to filter the data by a column? (yes/no): ").lower()
if filter_choice == 'yes':
print("\n🧩 Available columns:")
print(list(data.columns))
column_name = input("Enter the column name to filter by: ")
if column_name in data.columns:
filter_value = input(f"Enter the value to filter in '{column_name}': ")
# Step 6: Filter the data
filtered_data = data[data[column_name].astype(str) == filter_value]
print("\n✅ Filtered Data:")
print(filtered_data)
# Step 7 (Bonus): Save filtered data
save_choice = input("\nDo you want to save this filtered data? (yes/no): ").lower()
if save_choice == 'yes':
output_file = input("Enter a name for the new CSV file (e.g., result.csv): ")
filtered_data.to_csv(output_file, index=False)
print(f"📁 Saved to {output_file}")
else:
print("❌ Column not found.")
else:
print("👍 Done without filtering.")
except FileNotFoundError:
print("❌ File not found. Please check the path and try again.")
except pd.errors.ParserError:
print("❌ Could not parse the CSV file. Ensure it is properly formatted.")