Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas read_clipboard() Method



Theread_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 theread_csv() method. Sinceread_clipboard() internally uses theread_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 arenumpy_nullable andpyarrow.

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

Return Value

Theread_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 usingread_clipboard().

import pandas as pd# Creating a sample DataFramedf = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['A', 'B', 'C'])# Copy DataFrame to clipboarddf.to_clipboard()# Read data from clipboardclipboard_df = pd.read_clipboard()# Display the DataFrameprint('DataFrame from clipboard:')print(clipboard_df)

When we run above program, it produces following result −

DataFrame from clipboard:
ABC
0123
1456

Example: Importing Clipboard Data with Custom Separator

If the copied data contains different delimiter other thancomma(,), we can specify the separator by using thesep parameter of theread_clipboard() method.

Before executing the below code please copy the following data.

Index;A;B0;10;301;20;40
import pandas as pd# Read clipboard data with tab separatorclipboard_df = pd.read_clipboard(sep=';')# Display the DataFrameprint('DataFrame from clipboard:')print(clipboard_df)

While executing the above code we get the following output −

DataFrame from clipboard:
IndexAB
001030
112040

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  Cx  1  4  py  2  5  qz  3  6  r
import pandas as pd# Read clipboard dataclipboard_df = pd.read_clipboard()# Display the DataFrameprint('DataFrame from Clipboard:')print(clipboard_df)

Following is an output of the above code −

DataFrame from Clipboard:
ABC
x14p
y25q
z36r

Example: Reading Clipboard Data with Missing Values

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

Before executing the below code please copy the following data.

ID    Name    Score101   Kiara    85102   Rajesh    103   Adhya    78104   Saranya    92
import pandas as pd# Read the copied data from clipboarddf = pd.read_clipboard(sep="\\s+")# Display the DataFrameprint('DataFrame from Clipboard:')print(df)

Following is an output of the above code −

DataFrame from Clipboard:
IDNameScore
0101Kiara85.0
1102RajeshNaN
2103Adhya78.0
3104Saranya92.0
python_pandas_io_tool.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp