Team Ai
Apppublic

mrhammad12/y_data_profiling

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
data_loader.py58 linesDownload Raw Back to root
1import pandas as pd
2import streamlit as st
3
4def load_titanic_data():
5    """Load Titanic dataset with multiple fallback options"""
6    try:
7        # Try direct URL first
8        url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
9        df = pd.read_csv(url)
10        st.success("✅ Titanic dataset loaded from web")
11        return df
12    except:
13        try:
14            # Try seaborn as fallback
15            import seaborn as sns
16            df = sns.load_dataset('titanic')
17            st.success("✅ Titanic dataset loaded from seaborn")
18            return df
19        except:
20            # Final fallback: create sample data
21            st.warning("⚠️ Using sample Titanic data (web source unavailable)")
22            return pd.DataFrame({
23                'Survived': [0, 1, 1, 1, 0, 0, 1, 0, 1, 1],
24                'Pclass': [3, 1, 3, 1, 3, 3, 2, 3, 1, 3],
25                'Sex': ['male', 'female', 'female', 'female', 'male', 'male', 'female', 'male', 'female', 'female'],
26                'Age': [22.0, 38.0, 26.0, 35.0, 35.0, 28.0, 54.0, 2.0, 27.0, 14.0],
27                'Fare': [7.2500, 71.2833, 7.9250, 53.1000, 8.0500, 8.4583, 51.8625, 21.0750, 11.1333, 30.0708],
28                'Embarked': ['S', 'C', 'S', 'S', 'S', 'Q', 'S', 'S', 'S', 'C']
29            })
30
31def load_iris_data():
32    """Load Iris dataset with multiple fallback options"""
33    try:
34        # Try direct URL first
35        url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
36        df = pd.read_csv(url)
37        st.success("✅ Iris dataset loaded from web")
38        return df
39    except:
40        try:
41            # Try sklearn as fallback
42            from sklearn.datasets import load_iris
43            iris = load_iris()
44            df = pd.DataFrame(iris.data, columns=iris.feature_names)
45            df['species'] = iris.target
46            df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
47            st.success("✅ Iris dataset loaded from sklearn")
48            return df
49        except:
50            # Final fallback: create sample data
51            st.warning("⚠️ Using sample Iris data (web source unavailable)")
52            return pd.DataFrame({
53                'sepal_length': [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9],
54                'sepal_width': [3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1],
55                'petal_length': [1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5],
56                'petal_width': [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1],
57                'species': ['setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa']
58            })