隨手記
- 注意點(diǎn)
- &、| 是位運(yùn)算,and、or是邏輯運(yùn)算
- and\or 短路計(jì)算
1.在計(jì)算 a and b 時(shí),如果 a 是 False,則根據(jù)與運(yùn)算法則,整個(gè)結(jié)果必定為 False,因此返回 a;如果 a 是 True,則整個(gè)計(jì)算結(jié)果必定取決與 b,因此返回 b。- 在計(jì)算 a or b 時(shí),如果 a 是 True,則根據(jù)或運(yùn)算法則,整個(gè)計(jì)算結(jié)果必定為 True,因此返回 a;如果 a 是 False,則整個(gè)計(jì)算結(jié)果必定取決于 b,因此返回 b。
- and\or 短路計(jì)算
- is 判斷是否是否一個(gè)對(duì)象,== 比較值是否相等
- 若果 a is b 為真,則 a == b 亦為真
- return 只能用在function里
- 讀取excel中文亂碼,由于excel一般是gbk編碼無(wú)utf-8,pd.read_csv讀取時(shí)設(shè)置參數(shù)enconding='gbk'
- 字符串前加 r 可以防止轉(zhuǎn)義
- 中文字符前加u代表以u(píng)nicode格式存儲(chǔ)
- 刪除變量:del
- 數(shù)據(jù)讀取 encoding成 'utf_8_sig'
- 索引、視圖、副本
- .ix不會(huì)修改原df; .iloc.loc會(huì)修改原df
- http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
- &、| 是位運(yùn)算,and、or是邏輯運(yùn)算
內(nèi)置
-
List
- list相加相當(dāng)于合并list (list.extend())
> [] + [1] + [2,3] [1,2,3,4]
String
s.join() # 字符聯(lián)接
-
yield
- 迭代器(list,string,tuple),可迭代
- 生成器自動(dòng)實(shí)現(xiàn)了迭代器協(xié)議,是可迭代的對(duì)象。生成器可以一邊計(jì)算一邊生成,節(jié)省了內(nèi)存。
- 帶有 yield 的函數(shù)就是一個(gè)生成器函數(shù),它不會(huì)執(zhí)行任何函數(shù)代碼,直到對(duì)其調(diào)用 next()(在 for 循環(huán)中會(huì)自動(dòng)調(diào)用 next())才開(kāi)始執(zhí)行。每執(zhí)行到一個(gè) yield 語(yǔ)句就會(huì)中斷,并返回一個(gè)迭代值,下次執(zhí)行時(shí)從 yield 的下一個(gè)語(yǔ)句繼續(xù)執(zhí)行。
-
map(func, seq1[, seq2...])
- 將func作用于seq的每一個(gè)元素中,并以列表形式返回。
- func為None時(shí)相當(dāng)與zip()
-
reduce(func, seq[, int])
- reduce每次迭代將上一次的迭代(第一次為int, 若無(wú)int則取seq第一個(gè)元素)結(jié)果與下一個(gè)元素傳入func中。
- e.g. 階乘計(jì)算 reduce(lambda: x * y, range(1, n+1))
Ipython
ctrl / -- 注釋
- Magic Commands
%time 計(jì)算耗時(shí)
Pandas
- 空值處理
df.isnull().any() 是否存在空值
df.isnull().sum() 統(tǒng)計(jì)空值個(gè)數(shù)
df.fillna(value) 空值填充
# value : scalar, dict, Series, or DataFrame
# e.g. df.fillna(df.mean())
- 數(shù)據(jù)探查
df.info() 基本信息
df.describe() 基本統(tǒng)計(jì)信息
df.get_dtype_counts() 統(tǒng)計(jì)數(shù)據(jù)類型
df.select_dtype(include=['O', 'int64']) 選取特定類型數(shù)據(jù), include/exclude 的值為list
df.select_dtypes(include = ['O']).apply(lambda x: len(x.unique())) 變量不同值的個(gè)數(shù)
apply(pd.Series.nunique()) 變量不同值個(gè)數(shù) len(unique()) #faster
pd.Series.value_counts()
pd.Series.pct_change() # 環(huán)比
- 數(shù)據(jù)合并
pd.concat() # 按指定 axis 合并,axis=1代表列合并
pd.merge() # 按列索引合并(SQL)
pd.join() # 多用于 行index
- 數(shù)據(jù)處理
pd.replace() 值替換 # 可事先構(gòu)造mapping_dict = {'F1':{'A':0, 'B':1}, 'F2':{1:9, 2:8}}
pd.Series.map({})
df.drop_duplicates(subset=) 去除重復(fù)值
pd.pivot_table() 數(shù)據(jù)框重塑
pd.groupby().fun().unstack()
pd.rest_index()
- 可視化
df.hist() 繪制柱狀圖
df.plot.bar(figsize=(,), stacked=True)
df.plot(kind) # kind: 'line', 'bar', 'barh', 'kde'
fig, (axis1,axis2) = plt.subplots(1,2,figsize=(15,4)) # 多幅圖設(shè)置圖片坐標(biāo)
- 日期時(shí)間處理
pd.to_datetime(Series/String, error=) # 轉(zhuǎn)成日期格式
datetime.weekday(); datetime.weekday_name # 轉(zhuǎn)成星期
datetime.quarter(); # 季度
datetime.weekofyear(); # 周
pd.Series.dt.weekday # use .dt to access properties
pd.date_range(start=None, end=None, periods=None, freq='D')
numpy
- 數(shù)值計(jì)算
np.log1p() # Return the natural logarithm of one plus the input array
scipy
- 數(shù)據(jù)統(tǒng)計(jì)
scipy.stats.skew() 計(jì)算峰度
- sparse 數(shù)據(jù)處理
from scipy import sparse
sparse.hstack().tocsr() # stack sparse matrices horizontally
注: 稀疏矩陣存儲(chǔ): compressed sparse row (CSR),Coordinate(COO)