from Pandas as pd
- pd.read_csv()
- pd.read_excel('1.xlsx',sheet_name=['python','student'])
- pd.DataFrame(result, columns=head)
- pd.to_excel()
- pd.to_sql('test_cjk',engine [1],if_exists='replace',index = False)
- pd.date_range(start='20170102', periods=7, freq="y")
- pd.Timestamp('20170102')
- pd.isnull() # 检查空值
- pd.notnull()
from Numpy as np
- np.array([3] * 4, dtype='int32')
- np.random.randn(7,4) # 正太分布
- np.random.rand(20,5) # 20行5列
DataFrame
- df.head()
- df.tail()
- df.index
- df.columns
- df.values
- df.describe() # 查看数值型列的汇总统计
- df.shape # 查看行数和列数
- df.sort_index(axis=1, ascending=False)
- df.sort_values(by='B')
- df.rename(columns={'old_name': 'new_ name'}) # 选择性更改列名
- df.set_index('column_one') # 更改索引列
- df.to_dict('records') # 转化为字典 'dict', 'list', 'series', 'split', 'records', 'index'
- df.loc[行,列]
- df.loc['index_one'] # 按索引选取数据
- df[[列1,列2]],df[列]
- df.iloc[0,:] # 返回第一行
- df.iloc[0,0] # 返回第一列第一个元素
- df.dropna() # 删除所有包含空值的行
- df.dropna(axis=1) # 删除所有包含空值的列
- df.dropna(axis=1,thresh=n) # 删除所有小于n个非空值的行
- df.fillna(x) # 用x替换DF对象中所有的空值
- df[df[col] > 0.5]
- df.sort_values(col1, ascending=False)
- df.sort_values([col1, col2], ascending=[True, False])
- df.groupby(col) # df.groupby([col1, col2])
- df.apply(np.mean) # 对DF中的每一列应用函数
- df.max(),df.min(),df.std(),df.count()
Series
- s.astype(float) # 将Series中的数据类型更改为float类型
- s.replace(1,'one') # 用‘one’代替所有等于1的值
- s.replace([1,3],['one','three']) # 用'one'代替1,用'three'代替3
-
from sqlalchemy import create_engine engine = create_engine(f'mysql+pymysql://root:root@localhost:3306/test?charset=utf8', encoding="utf-8") ↩