« Pandas
excel | Boolen ( default True ) Provide separator for pasting in Excel file, provide string if set to False |
sep | String, default is \t , The Field delimiters |
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)
« Data input and output from Pandas DataFrame
« Pandas
read_clipboard()
read_html()
read_csv()
read_excel()
to_excel()