-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_sql.py
More file actions
37 lines (31 loc) · 1.16 KB
/
process_sql.py
File metadata and controls
37 lines (31 loc) · 1.16 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
import pandas as pd
import sqlite3
import sys
def process_sql(db_path, table_name):
# Connect to the SQLite database
conn = sqlite3.connect(db_path)
# Read the table into a DataFrame
df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)
# Display basic information about the data
print("Data Overview:")
print(df.info())
print("\nFirst 5 Rows:")
print(df.head())
# Calculate and display statistics for numeric columns
numeric_cols = df.select_dtypes(include=['number']).columns
if not numeric_cols.empty:
print("\nStatistics for Numeric Columns:")
for col in numeric_cols:
print(f"\nColumn: {col}")
print(f"Mean: {df[col].mean()}")
print(f"Median: {df[col].median()}")
print(f"Mode: {df[col].mode().values}")
else:
print("\nNo numeric columns found in the SQL table.")
# Close the connection
conn.close()
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python process_sql.py path/to/yourdatabase.db tablename")
else:
process_sql(sys.argv[1], sys.argv[2])