to_clipboard()

Pandas Pandas input output

excelBoolen ( default True ) Provide separator for pasting in Excel file, provide string if set to False
sepString, default is \t , The Field delimiters

  • to_clipboard() : DataFrame to Clipboard


We can copy DataFrame to clipboard. We will create one DataFrame by using a dictionary. From DataFrame we will write to clipboard.
import pandas as pd
import numpy as np
my_dict={
	'NAME':['Ravi','Raju','Alex'],
	'ID':[1,2,3],'MATH':[30,40,50],
	'ENGLISH':[20,70,41]
	}
my_data = pd.DataFrame(data=my_dict)
my_data.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
We can drop the index column
my_data.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 ','
my_data.to_clipboard(index=False,sep=',')
NAME,ID,MATH,ENGLISH
Ravi,1,30,20
Raju,2,40,70
Alex,3,50,41
By using excel=False we can get string output, this we can't paste in Excel file.
my_data.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"
my_data = pd.read_sql(sql,my_connect )
my_data.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()

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


plus2net.com



Post your comments , suggestion , error , requirements etc here





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-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer