
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Clipboard
Copying and pasting data between different applications is a common task in data analysis. In this, clipboard acts as a temporary data buffer that is used to store short-term data and transfer it between different applications like Excel, text editors, and Python scripts. The Pandas library provides easy tools to work with the system clipboard −
read_clipboard(): Reads clipboard data and converts it into a Pandas DataFrame.
to_clipboard(): Copies a DataFrame to the clipboard for pasting elsewhere.
These methods make it easy to transfer data between Pandas data structures and other applications like Excel, text editors, or any tool that supports copy-paste functionality.
In this tutorial, we will learn about how to use the Pandas read_clipboard() and to_clipboard() methods effectively.
Note: If you get the pandas.errors.PyperclipException Error then, you may need to install xclip or xsel modules to enable clipboard functionality. Generally, Windows and macOS operating systems do not require these modules.
Reading Clipboard Data using read_clipboard()
The pandas.read_clipboard() method is used to directly import data from your system clipboard into a Pandas DataFrame. This method parses the clipboard data similarly to how CSV data is parsed using the pandas.read_csv() method.
The syntax of the pandas.read_clipboard() method is as follows −
pandas.read_clipboard(sep='\\s+', dtype_backend=<no_default>, **kwargs)
Key parameters,
sep: This parameter is used to defines the string delimiter. By default it is set to '\s+', which matches one or more whitespace characters.
dtype_backend: This is used for selecting the back-end data type. For example, "numpy_nullable" returns a nullable-dtype-backed DataFrame (default), and "pyarrow" returns a pyarrow-backed nullable ArrowDtype DataFrame (introduced in Pandas 2.0).
**kwargs: Additional keyword arguments passed to read_csv() to fine-tune the data reading.
Example
Here is a basic example of using the pandas.read_clipboard() method to generate a DataFrame from the copied data. In this example, we initially created a clipboard data using the to_clipboard() method.
import pandas as pd # Creating a sample DataFrame df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['A', 'B', 'C']) # Copy DataFrame to clipboard df.to_clipboard() # Read data from clipboard clipboard_df = pd.read_clipboard() # Display the DataFrame print('DataFrame from clipboard:') print(clipboard_df)
When we run above program, it produces following result −
DataFrame from clipboard:
A | B | C | |
---|---|---|---|
0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 |
Reading Tabular Data from Clipboard
When clipboard data includes row and column labels, read_clipboard() automatically detects and converts it into a structured DataFrame.
Example
The following example demonstrates how to use the pandas.read_clipboard() method to generate a DataFrame from the copied tabular data.
First, copy the following data to your clipboard using the Ctrl+c (Windows/Linux) or Command-C (macOS) keyboard shortcut.
C1 C2 C3 X 1 2 3 Y 4 5 6 Z a b c
Then Run the following code −
import pandas as pd # Read clipboard content into a DataFrame df = pd.read_clipboard() print(df)
Following is the output of the above code −
C1 | C2 | C3 | |
---|---|---|---|
X | 1 | 2 | 3 |
Y | 4 | 5 | 6 |
Z | a | b | c |
Reading Non-Tabular Data from Clipboard
When you have a non-tabular data in your clipboard with a specific delimiter, you can use the sep parameter of the read_clipboard() method to read such a type of data into Pandas DataFrame.
Example
Below is an example that demonstrates how to read non-tabular clipboard data into a Pandas DataFrame using the pandas.read_clipboard() method.
Copy the following data to your clipboard, then run the program below −
Python,Pandas,Clipboard,DataFrame
import pandas as pd # Read clipboard content into a DataFrame df = pd.read_clipboard(sep=',',header=None) print(df)
Following is the output of the above code −
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | Python | Pandas | Clipboard | DataFrame |
Writing Data to Clipboard with to_clipboard()
The to_clipboard() method is used to write the content of a DataFrame or Series object to the system clipboard. This makes it easy to paste data into other applications, such as Excel or text editors.
Following is the syntax of the to_clipboard() method −
DataFrame.to_clipboard(*, excel=True, sep=None, **kwargs)
Parameters
excel: It is a boolean parameter, if set to True, formats the DataFrame as CSV for easy pasting into Excel. If False, formats the DataFrame as a string representation to the clipboard.
sep: Defines the field delimiter. If sep=None, it defaults to a tab (\t) delimiter.
**kwargs: Any Additional arguments will be passed to DataFrame.to_csv.
Example
Here is an example of copying a DataFrame to the clipboard using the DataFrame.to_clipboard() and pasting it elsewhere like text editors.
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.')
Following is the output of the above code −
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