算法-lowb三人組
阿新 • • 發佈:2018-02-01
2-2 創建方式 map函數 log owb inf type parse 方式
---恢復內容開始---
d定義:
pandas是一個強大的Python數據分析的工具包。
pandas是基於NumPy構建的。
安裝方法:
pip install pandas
import pandas as pd
pandas的主要功能
具備對其功能的數據結構DataFrame、Series
集成時間序列功能
提供豐富的數學運算和操作
靈活處理缺失數據
Series
定義:Series是一種類似於一位數組的對象,由一組數據和一組與之相關的數據標簽(索引)組成。
創建方式:
創建方式: pd.Series([4,7,-5,3]) pd.Series([4,7,-5,3],index=[‘a‘,‘b‘,‘c‘,‘d‘]) pd.Series({‘a‘:1, ‘b‘:2}) pd.Series(0, index=[‘a‘,‘b‘,‘c‘,‘d’])
獲取值數組和索引數組:values屬性和index屬性
Series比較像列表(數組)和字典的結合體。
實例:
sr=pd.Series([1,2,3,4],index=[‘a‘,‘b‘,‘c‘,‘d‘]) sr[‘a‘:‘c‘] ==> a -4 b 3 c 5 dtype: int64 sr[[判斷,切片,取值‘a‘,‘d‘]] == a -4 d 6 dtype: int64 判斷 條件是鍵不是值 ‘b‘ in sr == true 1 in sr == flase 取值: 取值的方法和字典相類似 sr.get(‘a‘,0)
sr=pd.Series([1,2,3,4],index=[‘b‘,‘c‘,‘d‘,‘a‘]) b 1 c 2 d 3 a 4 dtype: int64 sr.iloc[1] #取索引為1 == 2 sr.ilc[2] #取索引為2 == 3取索引
sr=pd.Series([1,2add方法和 a+b區別,3,4],index=[‘b‘,‘c‘,‘d‘,‘a‘]) sr1=pd.Series([5,6,7,8,9],index=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]) sr2=pd.Series([5,6,7,8,9,10],index=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘]) sr+sr1 == a 9.0 b 7.0 c 9.0 d 11.0 e NaN dtype: float64 PS:多出來的值只是NAN add方法 sr3=sr.add(sr2,fill_value=0) sr3: == a 9.0 b 7.0 c 9.0 d 11.0 e 9.0 f 10.0 dtype: float64 用add方法:沒有就加上,不會出現Nan
sr4 a 9.0 b 7.0 c 9.0 d 11.0 e NaN dtype: float64 sr4.notnull() a True b True c True d True e False dtype: bool sr4[sr4.notnull()] #把是NAN的去掉 a 9.0 b 7.0 c 9.0 d 11.0 sr4.dropna() #也是直接去掉為nan的 a 9.0 b 7.0 c 9.0 d 11.0 dtype: float64notnull()和dropna()
sr=pd.DataFrame({‘one‘:[1,2,3,4],‘two‘:[32,4,5,6]},index=[‘a‘,‘s‘,‘d‘,‘q‘])
import random li = [random.uniform(10,20) for _ in range(1000)] ratio = 6.3 list(map(lambda x:x*ratio, li))map函數調用
df = pd.read_csv(‘601318.csv‘, header=None, names=list(‘asdfghjk‘))
df = pd.read_csv(‘601318.csv‘,index_col=1, parse_dates=[‘date‘]) df
df.groupby(‘key1‘).sum()
---恢復內容結束---
算法-lowb三人組