机器学习-数据科学库(第四天)
23.pandas的series的了解
為什么要學習pandas
numpy能夠幫助我們處理數值,但是pandas除了處理數值之外(基于numpy),還能夠幫助我們處理其他類型的數據
pandas的常用數據類型
pandas之Series創建
import pandas as pd t1 = pd.Series([1,2,15,3,4]) print(t1) t2 = pd.Series([1,2,15,3,4],index=list("abcde")) print(t2)dict = {"name":"xiaohong","age":30,"tel":10086} t3 = pd.Series(dict) print(t3) 0 1 1 2 2 15 3 3 4 4 dtype: int64a 1 b 2 c 15 d 3 e 4 dtype: int64name xiaohong age 30 tel 10086 dtype: objectpandas之Series切片和索引
print(t3["age"]) print(t3[0]) print(t3[[0,2]]) 30xiaohongname xiaohong tel 10086 dtype: objectpandas之Series的索引和值
print(t3.index) print(t3.values) print(type(t3.values)) Index(['name', 'age', 'tel'], dtype='object')['xiaohong' 30 10086]<class 'numpy.ndarray'>24.pandas讀取外部數據
pandas之讀取外部數據
import pandas as pd #pandas讀取csv中的文件 df = pd.read_csv("")25.pandas的dataFrame的創建
pandas的dataFrame的創建
import pandas as pd import numpy as np #pandas讀取csv中的文件 t = pd.DataFrame(np.arange(12).reshape(3,4)) print(t) 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11DataFrame對象既有行索引,又有列索引
26 .Dataframe的描述信息
Dataframe的描述信息
import pandas as pd import numpy as np from pymongo import MongoClient client = MongoClient() collection = client["douban"]["tv1"] data = collection.find() data_list = [] for i in data:temp = {}temp["info"]=i["info"]temp["rating_count"]=i["rating"]["count"]temp["rating_value"] = i["rating"]["value"]temp["title"]=i["title"]temp["country"]=i["country"]temp["directors"]=i["directors"]temp["actors"] = i["actors"]data_list.append(temp) df=pd.DataFrame(data_list) print(df) import pandas as pd df = pd.read_csv("/Users/zhucan/Desktop/dogNames2.csv") print(df.head()) print(df.info())#=DataFrames排序的方法 df=df.sort_values(by="Count_AnimalName",ascending=True) print(df) Row_Labels Count_AnimalName 0 1 1 1 2 2 2 40804 1 3 90201 1 4 90203 1<class 'pandas.core.frame.DataFrame'> RangeIndex: 16220 entries, 0 to 16219 Data columns (total 2 columns):# Column Non-Null Count Dtype --- ------ -------------- ----- 0 Row_Labels 16217 non-null object1 Count_AnimalName 16220 non-null int64 dtypes: int64(1), object(1) memory usage: 253.6+ KB NoneRow_Labels Count_AnimalName 0 1 1 9383 MERINO 1 9384 MERISE 1 9386 MERLEDEZ 1 9389 MERLYN 1 ... ... ... 12368 ROCKY 823 3251 COCO 852 2660 CHARLIE 856 9140 MAX 1153 1156 BELLA 1195 [16220 rows x 2 columns]27.dataFrame的索引
pandas之取行或者列
import pandas as pd df = pd.read_csv("/Users/zhucan/Desktop/dogNames2.csv") #=DataFrames排序的方法 df=df.sort_values(by="Count_AnimalName",ascending=False)#pandas取行或者列的注意點 #方括號寫數組,表示取行,對行進行操作 #寫字符串,表示的是取列索引,對列進行操作 print(df[:20]) print(df[:20]["Row_Labels"])pandas之loc
import pandas as pd import numpy as np t3 =pd.DataFrame(np.arange(12).reshape(3,4),index=list("abc"),columns=list("WXYZ")) print(t3) print(t3.loc["a","Z"]) print(t3.loc["a"]) print(t3.loc[["a","b"]]) print(t3.loc[:,"W"]) print(t3.iloc[1]) W X Y Z a 0 1 2 3 b 4 5 6 7 c 8 9 10 1128.bool索引和缺失數據的處理
pandas之布爾索引
import pandas as pd df = pd.read_csv("/Users/zhucan/Desktop/dogNames2.csv") print(df[(800<df["Count_AnimalName"])&(df["Count_AnimalName"]<1000)])pandas之字符串方法?
df["Row_Labels"].str.len()>4
df["info"].str.split("/").tolist()
缺失數據的處理
我們的數據缺失通常有兩種情況:
判斷是否為NaN:pd.isnull(df)? ? ? 若是NaN返回True 若不是NaN返回False
處理方式1:刪除NaN所在的行列t3.dropna (axis=0, how='any')? 刪除存在NaN的行?
? ? ? ? ? ? ? ? ? ? 刪除NaN所在的行列t3.dropna (axis=0, how='all ') 刪除全為NaN的行
? ? ? ? ? ? ? ? ? ? t3.dropna (axis=0, how='any', inplace=True),就地修改t3
處理方式2:填充數據,t.fillna(t.mean()),? ? ? t.fiallna(t.median()),? ? ? ?t.fillna(0)
? ? ? ? ? ? ? ? ? ? t2["age"]=t2["age"].fillna(t2["age"].mean())
注意:
29.pandas的常用統計方法
pandas的常用統計方法
假設現在我們有一組從2006年到2016年1000部最流行的電影數據,我們想知道這些電影數據中評分的平均分,導演的人數等信息,我們應該怎么獲取?
import numpy as np import pandas as pd file_path="/Users/zhucan/Desktop/datasets_IMDB-Movie-Data.csv" df=pd.read_csv(file_path) print(df.info()) print(df.head(1))#獲取平均評分 print(df["Rating"].mean()) #導演人數 print(len(set(df["Director"].tolist()))) #獲取演員人數 temp_actors_list = df["Actors"].str.split(", ").tolist() actor_list = [i for j in temp_actors_list for i in j] print(len(set(actor_list)))30.電影數直方圖
電影數直方圖
對于這一組電影數據,如果我們想rating,runtime的分布情況,應該如何呈現數據?
import numpy as np import pandas as pd from matplotlib import pyplot as plt file_path="/Users/zhucan/Desktop/datasets_IMDB-Movie-Data.csv" df=pd.read_csv(file_path) print(df.info()) print(df.head(1))#rating,runtime分布情況 runtime_data = df["Runtime (Minutes)"].valuesmax_runtime = runtime_data.max() min_runtime = runtime_data.min()#計算組數 num_bin = (max_runtime-min_runtime)//5 plt.figure(figsize=(20,8),dpi=80) plt.hist(runtime_data,num_bin) plt.xticks(range(min_runtime,max_runtime+5,5)) plt.show()總結
以上是生活随笔為你收集整理的机器学习-数据科学库(第四天)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习-数据科学库(第三天)
- 下一篇: 机器学习-数据科学库(第五天)