Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython | Pandas dataframe.to_clipboard()

Python | Pandas dataframe.to_clipboard()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas dataframe.to_clipboard() function copy object to the system clipboard. This function writes a text representation of the object to the system clipboard. This could be easily pasted into excel or notepad++.

Syntax: DataFrame.to_clipboard(excel=True, sep=None, **kwargs)

Parameters :
excel : True, use the provided separator, writing in a csv format for allowing easy pasting into excel. False, write a string representation of the object to the clipboard.
sep : Field delimiter.
**kwargs : These parameters will be passed to DataFrame.to_csv.

Note : For link to the CSV file used in the code, click here

Example #1: Use to_clipboard() function to copy the object to the clipboard in a non-excel format.




# importing pandas as pd
import pandas as pd
  
# Creating the dataframe 
df = pd.read_csv('nba.csv')
  
# Print the dataframe
df


Output :

Now we will copy this object to clipboard in a non-excel format.




# copy to clipboard
df.to_clipboard(excel = False, sep = ', ')


Output :

We have simply pasted, what got copied to the clipboard after executing the previous command. The software used was “Notepad++”.
 
Example #2: Use to_clipboard() function to copy the object to the clipboard in an excel format.




# importing pandas as pd
import pandas as pd
  
# Creating the dataframe 
df = pd.read_csv('nba.csv')
  
# Print the dataframe
df


Output :

Now we will copy this object to clipboard in an excel format.




# copy to clipboard
df.to_clipboard(excel = True)


Output :

RELATED ARTICLES

Most Popular

Recent Comments