p

pandas

此程式碼用於取得目前 Python 環境中所有已安裝的套件名稱和版本,並將結果整理成 CSV 檔案,方便進行記錄、分享或系統排查。

0 0 0 1
1 # 引入需要用到的套件
2 import pandas as pd # 用來處理資料,還能幫我們存成 CSV 檔
3 from pkg_resources import working_set # 用來拿到所有已安裝的套件清單
4
5 # 步驟 1: 把所有已安裝的套件名稱和版本抓出來,直接變成 Pandas DataFrame
6 # 用列表生成式搭配 DataFrame,省去不必要的中間變數
7 df_packages = pd.DataFrame(
8 [(pkg.key, pkg.version) for pkg in working_set],
9 columns=["Package", "Version"]
10 ).sort_values(by="Package")

此範例展示如何使用 Streamlit 處理大量資料,並提供可視化表格 (dataframe) 和可編輯表格 (data_editor) 來動態調整資料。支援圖片預覽、進度條顯示及類別選擇,適用於資料分析與管理應用。

0 0 0 1
1 import streamlit as st
2 import pandas as pd
3 import numpy as np
4
5 st.write("Got lots of data? Great! Streamlit can show [dataframes](https://docs.streamlit.io/develop/api-reference/data) with hundred thousands of rows, images, sparklines – and even supports editing! ✍️")
6
7 num_rows = st.slider("Number of rows", 1, 10000, 500)
8 np.random.seed(42)
9 data = []
10 for i in range(num_rows):

此範例展示如何使用 Streamlit 建立互動式數據視覺化,包含多重選擇 (multiselect) 和切換開關 (toggle) 來調整數據顯示,並透過 line_chart 繪製折線圖,讓使用者輕鬆分析數據趨勢。

0 0 0 1
1 import streamlit as st
2 import pandas as pd
3 import numpy as np
4
5 st.write("Streamlit supports a wide range of data visualizations, including [Plotly, Altair, and Bokeh charts](https://docs.streamlit.io/develop/api-reference/charts). 📊 And with over 20 input widgets, you can easily make your data interactive!")
6
7 all_users = ["Alice", "Bob", "Charly"]
8 with st.container(border=True):
9 users = st.multiselect("Users", all_users, default=all_users)
10 rolling_average = st.toggle("Rolling average")

這段程式碼提供了一個 YAMLProcessor 類別,負責讀取和寫入 YAML 檔案(預設為 local_data.yaml),並將其內容轉換為 pandas.DataFrame 進行處理,適用於需要以表格形式操作 YAML 資料的應用場景。

1 0 0 1
1 import os
2 import yaml
3 import pandas as pd
4 import logging
5 from typing import Any, Dict, List
6
7 # 設定 logging 基本參數
8 logging.basicConfig(level=logging.INFO)
9 logger = logging.getLogger(__name__)

這段程式碼是一個使用 Streamlit 和 SQLite 的範例應用程式,透過 JOIN 查詢顯示客戶與訂單的關聯資料,並在介面中使用 st.data_editor 允許編輯訂單日期,按下更新按鈕後會將修改內容同步至資料庫。

0 0 0 1
1 import sqlite3
2 import pandas as pd
3 import streamlit as st
4 from datetime import datetime, date
5
6 def initialize_database(conn):
7 cursor = conn.cursor()
8
9 # 建立 Customers 資料表
10 cursor.execute('''

此程式使用 Streamlit 讀取、編輯並儲存員工資料至 YAML 檔案,提供直覺化 UI 介面,並支援欄位過濾與年齡篩選,適用於企業資料管理。

0 0 0 2
1 employees:
2 - Age: 34
3 City: New York
4 Name: Alice
5 Occupation: Engineer
6 Salary: 70000
7 - Age: 27
8 City: Los Angeles
9 Name: Bob
10 Occupation: Doctor