Preparation
Before any data manipulation can occur, two (2) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The Openpyxl library enables conversion to/from Excel.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter> key on the keyboard to start the installation process.
$ pip install openpyxl
Hit the <Enter> key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import openpyxl
DataFrame.to_clipboard()
The to_clipboard
method copies an object to the operating system’s clipboard. The output can be pasted (WIndows: CTRL+V
) to other applications.
The syntax for this method is as follows:
DataFrame.to_clipboard(excel=True, sep=None, **kwargs)
Parameter | Description |
---|---|
excel | If this parameter is True , the output is saved in a CSV format for pasting to Excel. |
sep | This is the field separator between the fields. The default value is a comma. |
**kwargs | These parameters will pass to a DataFrame’s to_csv() method. |
This example copies the inventory for Rivers Clothing to the system clipboard. A Notepad application is opened and the contents of the clipboard contents pasted.
df_inv = pd.DataFrame({'Tops': [36, 23, 19], 'Tanks': [20, 10, 20], 'Pants': [61, 33, 67], 'Sweats': [88, 38, 13]}) df_inv.to_clipboard(sep=',', index=False)
- Line [1] creates a DataFrame from a dictionary of lists. The output saves to the
df_inv
. - Line [2] does the following:
- copies the contents to the clipboard
- separates the fields with the comma (
,
) character - omits the leading comma for each row (
index=False
)
To see this code in action, perform the following steps:
- Navigate to and open an instance of Notepad (or another text editor).
- Press
CTRL+V
(Windows) to paste the contents of the system clipboard to the application.
Output
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.