2020-09-17 學習總結(jié)

一、python連接數(shù)據(jù)庫
例如連接以下數(shù)據(jù)庫:
數(shù)據(jù)庫類型:MYSQL數(shù)據(jù)庫(主數(shù)據(jù)庫)
IP地址:127.0.0.0
用戶名:frog
密碼:Frog
數(shù)據(jù)庫實例名:frogdata
1、python 連接mysql

import pymysql
cons = pymysql.connectt(
host = '127.0.0.0',
user='frog',
password='Frog', 
db = 'frogdata',    # 數(shù)據(jù)庫
port =3306,   #端口
charset = 'utf8'
)
cur = cons.cursor()    #創(chuàng)建游標
cur.execute('select * from orderinfo')   #存儲結(jié)果
data = cur.fetchall()  #調(diào)用結(jié)果
data   #查看數(shù)據(jù),元組
cons.commit()    #增刪改查
cur.close()
cons.close()  #關(guān)閉游標和數(shù)據(jù)庫

2.pandas連接mysql
a、連接遠程數(shù)據(jù)庫
engine = create_engine("數(shù)據(jù)庫類型+數(shù)據(jù)庫驅(qū)動://數(shù)據(jù)庫用戶名:數(shù)據(jù)庫密碼@IP地址:端口/數(shù)據(jù)庫?charset=編碼格式")
mysql 默認端口3306

import pandas as pd
import pymysql
from sqlalchemy import create_engine
engine=create_engine('mysql+pymysql://frog:Frog@127.0.0.0:3306/frogdata?charset=utf8')
sql_cmd = 'select * from orderinfo'
data=pd.read_sql(sql = sql_cmd, con = engine)

b. 連接本地數(shù)據(jù)庫
將上述讀取的data 表存儲到本地數(shù)據(jù)庫test中,表名為data

engines=create_engine("mysql+pymysql://root:mysql@localhost/test")
data.to_sql(name = 'data', con = engine1)

二、查看數(shù)據(jù)類型的幾種方式
1、python 自帶函數(shù) type()

import numpy as np
import pandas as pd

a = np.array([[1,2,3],[2,3,4]])
b = pd.DataFrame(a)
print(type(a))
print(type(b))

<class 'numpy.ndarray'>
<class 'pandas.core.frame.DataFrame'>
2、pandas 函數(shù) info()

print(b.info())

結(jié)果:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):……

三、時間轉(zhuǎn)字符串類型等,延伸時間函數(shù)總結(jié)
1、datetime轉(zhuǎn)換為字符串,傳遞指定格式給strftime

from datetime import datetime
stamp = datetime(2020,9,21)
stamp.strftime('%Y-%m')

輸出結(jié)果:
'2020-09'
2、字符串轉(zhuǎn)換為datetime, datetime.strptime

value = '2020/09/21'
stamp.strptime(value,'%Y/%m/%d')

輸出結(jié)果:
datetime.datetime(2020, 9, 21, 0, 0)

四、DataFrame中存在科學計數(shù)法
1、字符串

a = '1.23456789e6'
float(a)
# eval(a)

兩個函數(shù)結(jié)果相同
1234567.89

五、其他
1、指定輸出小數(shù)位數(shù)
print('%.4f' %a)
2、同時輸入多個數(shù)字
num = list(map(int,input(‘請輸入多個數(shù)字,中間為空格:’).split()))
3、棧中的元素按原順序輸出
s = Stack()
s.push('a')
s.push('b')
s.push('c')
print(''.join(s.items))
4、棧中的元素按逆序輸出
s = Stack()
s.push('a')
s.push('b')
s.push('c')
strs.append(s.pop())
print(''.join(strs.items))

5、計算上下兩行之間相除或相乘等一系列方法的函數(shù)使用方法
diff() shift()

df = pd.Series([1,3,7,4,9])
print(df.shift(3))
print(df.diff(2))

結(jié)果:
0 NaN
1 NaN
2 NaN
3 1.0
4 3.0
dtype: float64
0 NaN
1 NaN
2 6.0
3 1.0
4 2.0
dtype: float64
0 1
1 3
2 7
3 4
4 9
dtype: int64

6、總結(jié)DataFrame與Series類型區(qū)別
Series是一維的數(shù)組型對象
DataFrame是二維表結(jié)構(gòu)

7、總結(jié)DataFrame專用的排序函數(shù)和列表的排序函數(shù)
DataFrame:
(1)按行或列索引進行字典型排序,使用sort_index, axis指定行或列索引
ascending 指定升降序排列

obj = pd.DataFrame(np.arange(8).reshape((2,4)),
                   index=['two', 'one'],
                   columns=['d','a','b','c'])
print(obj)
print(obj.sort_index(axis=1))

結(jié)果:
d a b c
two 0 1 2 3
one 4 5 6 7
a b c d
two 1 2 3 0
one 5 6 7 4
(2)使用1列或多列作為排序鍵,sort_values

obj = pd.DataFrame({'b':[4,7,-3,2],'a':[0,1,0,1]})
print(obj)
print(obj.sort_values(by='b'))
print(obj.sort_values(by=['a','b']))

結(jié)果:
b a
0 4 0
1 7 1
2 -3 0
3 2 1
b a
2 -3 0
3 2 1
0 4 0
1 7 1
b a
2 -3 0
0 4 0
3 2 1
1 7 1

list: sorted(a) 是新建已排序的列表, a.sort() 對a進行排序

a = [7,2,5,1,3]
a.sort()
print(a)

結(jié)果:[1, 2, 3, 5, 7]

8、總結(jié)存儲到mysql的幾種形式(覆蓋、追加等)
engine = create_engine("數(shù)據(jù)庫類型+數(shù)據(jù)庫驅(qū)動://數(shù)據(jù)庫用戶名:數(shù)據(jù)庫密碼@IP地址:端口/數(shù)據(jù)庫?charset=編碼格式")
df.to_sql(name, con, flavor=None, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None)
if_exists:
-fail',若表存在,不修改

  • 'replace',若表存在,則刪除,重新創(chuàng)建表并追加數(shù)據(jù),即覆蓋原表
  • 'append',若表存在,追加數(shù)據(jù);不存在則創(chuàng)建
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容