[Python data analysis] pandas data import

Import CSV file data

environment

C:\Users\Thinkpad\Desktop\Data\InfoTable.csv

image


grammar

pd.read_csv(filename): import data from CSV file


implementation code

import pandas as pd
f = open("C:/Users/Thinkpad/Desktop/Data/信息表.csv",encoding="utf-8")
content = pd.read_csv(f)
print(content)


operation result

image


Import Excel file data

environment

C:\Users\Thinkpad\Desktop\Data\InfoTable.xlsx

image

grammar

pd.read_excel(filename): Import data from an Excel file


implementation code

import pandas as pd
f = " C:/Users/Thinkpad/Desktop/Data/InfoTable.xlsx "
content = pd.read_excel(f)
print(content)


operation result

image

Import Mysql data table data

environment

CREATE TABLE TB(ID INT,NAME VARCHAR(20));
INSERT INTO TB VALUES(1,'张三');

Database name: mis User name: root Password: root

use command

pip install pymysql

image


grammar

pd.read_sql(query, connection_object): import data from SQL table/library


implementation code

import pymysql
import pandas as pd

conn = pymysql.connect(host='localhost',user='root',password='root',db='mis',charset='utf8')
content = pd.read_sql('select * from TB',con=conn)
print(content)
conn.close()
Notice:

In the image above we can see that the encoding of the link is: UTF8

operation result

image


Import text file data

environment

C:\Users\Thinkpad\Desktop\Data\InfoTable.txt

image

The delimiter is: comma


grammar

pd.read_table(filename): Import data from a delimited text file


implementation code

import pandas as pd
f = open("C:/Users/Thinkpad/Desktop/Data/信息表.txt",encoding='utf8')
content = pd.read_table(f,",")
print(content)


operation result

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325035969&siteId=291194637