Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Pandas read_clipboard() Method



The read_clipboard() method in Python's Pandas library provides an easy way to read data copied to the system clipboard and directly convert it into a Pandas DataFrame. This functionality is particularly useful for quickly importing tabular data from other sources such as Microsoft Excel, Google Sheets, or web pages.

This method initially reads the text from clipboard and pass it to the read_csv() method. Since read_clipboard() internally uses the read_csv() method, it supports many of the same arguments, making it a flexible tool for data parsing.

Syntax

Following is the syntax of the Python Pandas read_clipboard() method −

pandas.read_clipboard(sep='\\s+', dtype_backend=<no_default>, **kwargs)

Parameters

The read_clipboard() method accepts the following parameters −

  • sep: A string or regex delimiter that separates values. By default it is set to '\\s+', which denotes one or more whitespace characters.

  • dtype_backend: Determines the backend data type for the resultant DataFrame. Available options are numpy_nullable and pyarrow.

  • **kwargs: Additional arguments that can be passed to read_csv() for customization.

Return Value

The read_clipboard() method returns a DataFrame object containing the parsed clipboard content.

Example: Reading a DataFrame from Clipboard

The following example demonstrates how to copy a Pandas DataFrame to the clipboard and read it back using read_clipboard().

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

Example: Importing Clipboard Data with Custom Separator

If the copied data contains different delimiter other than comma(,), we can specify the separator by using the sep parameter of the read_clipboard() method.

Before executing the below code please copy the following data.

Index;A;B
0;10;30
1;20;40
import pandas as pd

# Read clipboard data with tab separator
clipboard_df = pd.read_clipboard(sep=';')

# Display the DataFrame
print('DataFrame from clipboard:')
print(clipboard_df)

While executing the above code we get the following output −

DataFrame from clipboard:
Index A B
0 0 10 30
1 1 20 40

Example: Reading Clipboard Data with Index

If the copied data contains row labels, Pandas will recognize them as an index and converts it into Pandas DataFame as it is.

Before executing the below code please copy the following data.

   A  B  C
x  1  4  p
y  2  5  q
z  3  6  r
import pandas as pd

# Read clipboard data
clipboard_df = pd.read_clipboard()

# Display the DataFrame
print('DataFrame from Clipboard:')
print(clipboard_df)

Following is an output of the above code −

DataFrame from Clipboard:
A B C
x 1 4 p
y 2 5 q
z 3 6 r

Example: Reading Clipboard Data with Missing Values

If the clipboard data contains missing values, Pandas will automatically represent them as NaN when using the read_clipboard() method. The following example demonstrates how Pandas read_clipboard() method handles missing values in the copied data.

Before executing the below code please copy the following data.

ID    Name    Score
101   Kiara    85
102   Rajesh    
103   Adhya    78
104   Saranya    92
import pandas as pd

# Read the copied data from clipboard
df = pd.read_clipboard(sep="\\s+")

# Display the DataFrame
print('DataFrame from Clipboard:')
print(df)

Following is an output of the above code −

DataFrame from Clipboard:
ID Name Score
0 101 Kiara 85.0
1 102 Rajesh NaN
2 103 Adhya 78.0
3 104 Saranya 92.0
python_pandas_io_tool.htm
Advertisements