https://www.cnblogs.com/guxh/p/8627251.html
https://blog.csdn.net/yiyele/article/details/80605909
https://www.jianshu.com/p/f5c882073a1b
https://blog.csdn.net/ACID_lv_ing/article/details/87261787
建立数据库连接,并读取数据
import pymysql
import pandas as pd
# 连接数据库
#db,即数据库的名字,本次使用【well数据库】
conn = pymysql.connect(host='localhost',port=3306, user='root',
password='c7238125',db='well',charset='utf8',)
#表名字
table_name="虚拟_train"
sqlstring = "select * from %s" % table_name # table_name=>表名
df = pd.read_sql(sqlstring, conn)
print("type:",type(df))
结果:
type: <class ‘pandas.core.frame.DataFrame’>
获取df前5行数据
print(df.head())
结果:
读取表行列数→维度
维度=df.shape
print(维度)
结果:(944, 12)
读取表头
df_head=df.columns
print(type(df_head))
print(df_head)
df_head1=df.columns[2:11]
print(df_head1)
结果:
<class ‘pandas.core.indexes.base.Index’>
Index([‘数据ID’, ‘深度’, ‘AC’, ‘SP’, ‘GR’, ‘RXO’, ‘RI’, ‘RT’, ‘CALI’, ‘CNL’, ‘DEN’, ‘TOC’],dtype=‘object’)
Index([‘AC’, ‘SP’, ‘GR’, ‘RXO’, ‘RI’, ‘RT’, ‘CALI’, ‘CNL’, ‘DEN’], dtype=‘object’)
df_head=df.columns
print(type(df_head))
print(df_head)
读取输入变量和目标变量
#读取输入变量
input_variables=df[df_head1]
print(input_variables.head())
#读取目标变量
target_variable=df[["TOC"]]
print(target_variable.head())
结果:
输入变量
目标变量
读取value
df_value=df.values
print(df_value)
结果:
读取目标变量的value
语法:df.列名
target_variable_value=df.TOC
print(type(target_variable_value))
print(target_variable_value)
结果:<class ‘pandas.core.series.Series’>
显示print(target_variable_value)部分结果