to_clipboard(): DataFrame to clipboard ( copy and paste )

import pandas as pd
my_dict={
	'NAME':['Ravi','Raju','Alex'],
	'ID':[1,2,3],'MATH':[30,40,50],
	'ENGLISH':[20,70,41]
	}
df = pd.DataFrame(data=my_dict)
df.to_clipboard()
This is the data written to clipboard. We can paste the same in Excel file .
	NAME	ID	MATH	ENGLISH
0	Ravi	1	30	20
1	Raju	2	40	70
2	Alex	3	50	41


to_clipboard(): Copying DataFrame to Clipboard #B11


excelBoolen ( default True ) Provide separator for pasting in Excel file, provide string if set to False
sepString, default is \t , The Field delimiters
We can drop the index column
df.to_clipboard(index=False)
Output
NAME	ID	MATH	ENGLISH
Ravi	1	30	20
Raju	2	40	70
Alex	3	50	41

Using sep=','

By default sep='\t', we can change this to ','
df.to_clipboard(index=False,sep=',')
NAME,ID,MATH,ENGLISH
Ravi,1,30,20
Raju,2,40,70
Alex,3,50,41

excel

By using excel=False ( default value is True ) we can get string output, this we can't paste in Excel file ( to match the cells ) .
df.to_clipboard(index=False,excel=False)

Data from MySQL table to html file

Read on how to connect to MySQL database and then collected the records of student table by using read_sql() to a DataFrame. Finally we will copy the data to clip board by using to_clipboard().
import mysql.connector
import pandas as pd 
my_connect = mysql.connector.connect(
      host="localhost",
      user="root",
      passwd="*****",
      database="my_tutorial"
    )
####### end of connection ####
sql="SELECT * FROM student limit 0,5"
df = pd.read_sql(sql,my_connect )
df.to_clipboard(index=False)

Using SQLAlchemy

We will collect records from our sample student table in MySQL database and copy the output to Clipboard by using to_clipboard().
Collect SQL dump of sample student table below.
Read more on MySQL with SQLAlchemy connection.
import pandas as pd 
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student LIMIT 0,10 "
df = pd.read_sql(sql,my_conn)
df.to_clipboard()

From Excel file to Clipboard as output

In above code we have created one DataFrame by taking data from a MySQL database table. We can create DataFrame by using any excel data or by using any csv file or from any other sources. ( check here to create a DataFrame from 8 different sources )
Once a DataFrame is created, then using that we can copy output to Clipboard by using to_clipboard(). Here is one example to read one Excel file to a DataFrame and generate the output to Clipboard, you can paste the clipboard data to text file or Excel file.
We used read_excel() to read our sample student.xlsx file.
df=pd.read_excel("D:\\my_data\\student.xlsx") # Path of the file. 
df.to_clipboard()
We can read one csv file by using read_csv()
df=pd.read_csv("D:\\my_data\\student.csv") # change the path
df.to_clipboard()

Questions



Python Pandas DataFrame data to Clipboard by to_clipboard() MySQL sample table or Excel as source



Data input and output from Pandas DataFrame
Pandas read_clipboard() read_html() read_csv() read_excel() to_excel()

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer