
- Python Pandas 教程
- Python Pandas - 首頁
- Python Pandas - 簡介
- Python Pandas - 環境設定
- Python Pandas - 基礎
- Python Pandas - 資料結構介紹
- Python Pandas - 索引物件
- Python Pandas - 面板 (Panel)
- Python Pandas - 基本功能
- Python Pandas - 索引和資料選擇
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Series 物件切片
- Python Pandas - Series 物件屬性
- Python Pandas - Series 物件的算術運算
- Python Pandas - 將 Series 轉換為其他物件
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - 訪問 DataFrame
- Python Pandas - DataFrame 物件切片
- Python Pandas - 修改 DataFrame
- Python Pandas - 從 DataFrame 中刪除行
- Python Pandas - DataFrame 的算術運算
- Python Pandas - I/O 工具
- Python Pandas - I/O 工具
- Python Pandas - 使用 CSV 格式
- Python Pandas - 讀取和寫入 JSON 檔案
- Python Pandas - 從 Excel 檔案讀取資料
- Python Pandas - 將資料寫入 Excel 檔案
- Python Pandas - 使用 HTML 資料
- Python Pandas - 剪貼簿
- Python Pandas - 使用 HDF5 格式
- Python Pandas - 與 SQL 的比較
- Python Pandas - 資料處理
- Python Pandas - 排序
- Python Pandas - 重新索引
- Python Pandas - 迭代
- Python Pandas - 連線
- Python Pandas - 統計函式
- Python Pandas - 描述性統計
- Python Pandas - 處理文字資料
- Python Pandas - 函式應用
- Python Pandas - 選項和自定義
- Python Pandas - 視窗函式
- Python Pandas - 聚合
- Python Pandas - 合併/連線
- Python Pandas - 多級索引 (MultiIndex)
- Python Pandas - 多級索引基礎
- Python Pandas - 使用多級索引進行索引
- Python Pandas - 使用多級索引進行高階重新索引
- Python Pandas - 重新命名多級索引標籤
- Python Pandas - 對多級索引進行排序
- Python Pandas - 二元運算
- Python Pandas - 二元比較運算
- Python Pandas - 布林索引
- Python Pandas - 布林掩碼
- Python Pandas - 資料重塑和透視
- Python Pandas - 透視表
- Python Pandas - 堆疊和取消堆疊
- Python Pandas - 熔化 (Melting)
- Python Pandas - 計算虛擬變數
- Python Pandas - 分類資料
- Python Pandas - 分類資料
- Python Pandas - 分類資料的排序和排序
- Python Pandas - 分類資料的比較
- Python Pandas - 處理缺失資料
- Python Pandas - 缺失資料
- Python Pandas - 填充缺失資料
- Python Pandas - 缺失值的插值
- Python Pandas - 刪除缺失資料
- Python Pandas - 使用缺失資料進行計算
- Python Pandas - 處理重複項
- Python Pandas - 重複資料
- Python Pandas - 計數和檢索唯一元素
- Python Pandas - 重複標籤
- Python Pandas - 分組和聚合
- Python Pandas - GroupBy
- Python Pandas - 時間序列資料
- Python Pandas - 日期功能
- Python Pandas - 時間增量 (Timedelta)
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 警告和陷阱
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Python Pandas - 剪貼簿
在資料分析中,在不同應用程式之間複製和貼上資料是一項常見任務。在這個剪貼簿充當臨時資料緩衝區,用於儲存短期資料並在不同應用程式之間傳輸它。
Pandas 庫提供了易於使用的工具來處理剪貼簿。它提供了read_clipboard() 和 to_clipboard() 方法,分別用於從剪貼簿讀取資料和將資料寫入系統剪貼簿。這些方法簡化了 Pandas 資料結構與其他應用程式(如 Excel、文字編輯器或任何支援複製貼上功能的工具)之間的資料傳輸。
如果您遇到pandas.errors.PyperclipException 錯誤,則可能需要安裝xclip 或 xsel 模組才能啟用剪貼簿功能。通常,Windows 和 macOS 作業系統不需要這些模組。
在本教程中,我們將學習如何有效地使用 Pandas 的read_clipboard() 和 to_clipboard() 方法。
使用 read_clipboard() 從剪貼簿讀取資料
pandas.read_clipboard() 方法用於直接將系統剪貼簿中的資料匯入 Pandas DataFrame。此方法解析剪貼簿資料的方式類似於使用pd.read_csv() 方法解析 CSV 資料的方式。
pandas.read_clipboard() 方法的語法如下:
pandas.read_clipboard(sep='\\s+', dtype_backend=<no_default>, **kwargs)
其中:
sep:此引數用於定義字串分隔符。預設設定為'\s+',它匹配一個或多個空格字元。
dtype_backend:用於選擇後端資料型別。例如,“numpy_nullable”返回一個支援空型別的 DataFrame(預設值),而“pyarrow”返回一個 pyarrow 支援的空值 ArrowDtype DataFrame(在 Pandas 2.0 中引入)。
**kwargs**:傳遞給read_csv() 的其他關鍵字引數,用於微調資料讀取。
示例:從剪貼簿讀取表格資料
這是一個使用pandas.read_clipboard() 方法從複製的資料生成 DataFrame 的基本示例。
首先,請使用Ctrl+c 或Command-C 鍵盤快捷鍵將以下資料複製到剪貼簿。
C1 C2 C3 X 1 2 3 Y 4 5 6 Z a b c
然後執行以下程式碼:
import pandas as pd # Read clipboard content into a DataFrame df = pd.read_clipboard() print(df)
以下是上述程式碼的輸出:
C1 | C2 | C3 | |
---|---|---|---|
X | 1 | 2 | 3 |
Y | 4 | 5 | 6 |
Z | a | b | c |
示例:從剪貼簿讀取非表格資料
以下是如何使用pandas.read_clipboard() 方法將剪貼簿資料讀取到 pandas DataFrame 的示例。
讓我們將以下資料複製到剪貼簿,然後執行以下程式:
Python,Pandas,Clipboard,DataFrame
import pandas as pd # Read clipboard content into a DataFrame df = pd.read_clipboard(sep=',',header=None) print(df)
以下是上述程式碼的輸出:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | Python | Pandas | Clipboard | DataFrame |
使用 to_clipboard() 將資料寫入剪貼簿
to_clipboard() 方法用於將 DataFrame 或 Series 物件的內容寫入系統剪貼簿。這使得將資料貼上到其他應用程式(如 Excel 或文字編輯器)變得很容易。
以下是to_clipboard() 方法的語法:
DataFrame.to_clipboard(*, excel=True, sep=None, **kwargs)
其中:
excel:這是一個布林引數,如果設定為True,則將 DataFrame 格式化為 CSV,以便輕鬆貼上到 Excel 中。如果為False,則將 DataFrame 格式化為字串表示形式到剪貼簿。
sep:定義欄位分隔符。如果 sep=None,則預設為製表符 (\t) 分隔符。
**kwargs**:任何其他引數都將傳遞給 DataFrame.to_csv。
示例
這是一個使用DataFrame.to_clipboard() 將 DataFrame 複製到剪貼簿並在其他地方(如文字編輯器)貼上它的示例。
import pandas as pd # Create a DataFrame df = pd.DataFrame({ "C1": [1, 2, 3], "C2": [4, 5, 6], "C3": ["a", "b", "c"] }, index=["x", "y", "z"]) # Copies the DataFrame to the clipboard df.to_clipboard(sep=',') print('DataFrame is successfully copied to the clipboard. Please paste it into any text editor or Excel sheet.')
以下是上述程式碼的輸出:
DataFrame is successfully copied to the clipboard. Please paste it into any text editor or Excel sheet. ,C1,C2,C3 x,1,4,a y,2,5,b z,3,6,c