數據分析(2)

缺失值處理

-使用reindex()可以改變指定軸上的索引進行改變/增加/刪除操作

如:
df1=df.reindex(index=dates[0:4],colimn=list(df.column)+['E'])

-去掉包含缺失值的行,不改變原來的值

df1.dropna(how='any')

-對缺失值進行填充

df1.fillna(value=5)

-對數據進行布爾填充

pd.isnull(df1)

-統計

df.mean()

df.apply(計算函數)#優先在列上進行相應計算

-直方圖

s.value_counts()#計算相應元素的個數‘

-字符串辦法

把Series對象當作字符串然後進行大小寫等字符串的操作,如:

s.str.lower()#將元素全部變爲小寫

-合併

pd.concat(list)#合併相應DataFramee對象

pd.merge(合併對象,合併對象,on=column_index)#按相應column_index位置,將合併對象在column_index列上相等的值,取相等值的行進行合併

df.append(s)#將s合併於df上

-分組

df.group_by(column_index).sum()#將相應列上相同值進行合併分組,然後進行求和計算

-數據透視表

http://ww4.sinaimg.cn/mw690/6941baebgw1eqeocd4b5hj20i00nrdia.jpg

-時間序列

--針對頻率轉換進行重採樣

--時區表示

rng=pd.date_range('3/6/2012 00:00',periods=5,freq='D')
ts=pd.Series(np.random.randn(len(rang)),rang)
ts_utc=ts.ts_localize('UTC')#此處進行時區表示

--時區轉換

ts_utc.tz_convert('US/Eastern')

--時間跨度轉換

ps=ts.to_period()#轉換爲連續月
ps=ts.to_timestamp()#轉換爲連續月初
-Categorical

--將原始的grade轉換爲Categotical數據類型

df['column_index'].astype('category')
--Categorical重命名

df['column_index'].cat.categories=['a','b','c','d']#將Categorical數據類型改爲更有意義的名稱如abcd
--對Categorical列進行排序存在空的類別

df.groupby("grade").size()

導入和保存數據

-寫入CSV

df.to_csv('文件名')#寫入文件名.csv

-讀取CSV

pd.read_csv('文件名')#讀取文件

-寫入excel於讀取excel

df.to_excel('文件名',sheet_name='Sheet1')

pd.read_excel

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章