Spaces:
Sleeping
Sleeping
Update tools/excel_tool.py
Browse files- tools/excel_tool.py +32 -5
tools/excel_tool.py
CHANGED
@@ -1,17 +1,44 @@
|
|
1 |
import pandas as pd
|
2 |
import requests
|
3 |
from io import BytesIO
|
|
|
4 |
|
5 |
def analyze_excel(path_or_url: str) -> str:
|
|
|
6 |
try:
|
7 |
if path_or_url.startswith("http"):
|
8 |
-
response = requests.get(path_or_url)
|
|
|
9 |
df = pd.read_excel(BytesIO(response.content))
|
10 |
else:
|
11 |
df = pd.read_excel(path_or_url)
|
|
|
12 |
except Exception as e:
|
13 |
return f"Error reading Excel file: {e}"
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import requests
|
3 |
from io import BytesIO
|
4 |
+
import numpy as np
|
5 |
|
6 |
def analyze_excel(path_or_url: str) -> str:
|
7 |
+
"""Analysera Excel-fil och returnera detaljerad information"""
|
8 |
try:
|
9 |
if path_or_url.startswith("http"):
|
10 |
+
response = requests.get(path_or_url, timeout=10)
|
11 |
+
response.raise_for_status()
|
12 |
df = pd.read_excel(BytesIO(response.content))
|
13 |
else:
|
14 |
df = pd.read_excel(path_or_url)
|
15 |
+
|
16 |
except Exception as e:
|
17 |
return f"Error reading Excel file: {e}"
|
18 |
+
|
19 |
+
# Detaljerad analys
|
20 |
+
analysis = []
|
21 |
+
analysis.append(f"Rows: {df.shape[0]}, Columns: {df.shape[1]}")
|
22 |
+
analysis.append(f"Column names: {', '.join(df.columns)}")
|
23 |
+
|
24 |
+
# Datatyper
|
25 |
+
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
|
26 |
+
if numeric_cols:
|
27 |
+
analysis.append(f"Numeric columns: {', '.join(numeric_cols)}")
|
28 |
+
for col in numeric_cols:
|
29 |
+
analysis.append(f"{col} - Sum: {df[col].sum():.2f}, Mean: {df[col].mean():.2f}, Max: {df[col].max():.2f}, Min: {df[col].min():.2f}")
|
30 |
+
|
31 |
+
text_cols = df.select_dtypes(include=['object']).columns.tolist()
|
32 |
+
if text_cols:
|
33 |
+
analysis.append(f"Text columns: {', '.join(text_cols)}")
|
34 |
+
for col in text_cols:
|
35 |
+
unique_values = df[col].nunique()
|
36 |
+
analysis.append(f"{col} - Unique values: {unique_values}")
|
37 |
+
if unique_values <= 10:
|
38 |
+
analysis.append(f"{col} values: {', '.join(map(str, df[col].unique()))}")
|
39 |
+
|
40 |
+
# Första 3 raderna som exempel
|
41 |
+
analysis.append("First 3 rows:")
|
42 |
+
analysis.append(df.head(3).to_string())
|
43 |
+
|
44 |
+
return "\n".join(analysis)
|