摘要:引入包和加載數(shù)據(jù)清洗數(shù)據(jù)查看數(shù)據(jù)維度以及類型缺失值處理查看數(shù)據(jù)統(tǒng)計(jì)信息數(shù)值屬性離散化計(jì)算特征與屬性之間關(guān)系查看數(shù)據(jù)維度以及類型查看前五條數(shù)據(jù)查看每列數(shù)據(jù)類型以及情況獲得所有屬性查看數(shù)據(jù)統(tǒng)計(jì)信息查看連續(xù)數(shù)值屬性基本統(tǒng)計(jì)情況查看屬性數(shù)據(jù)統(tǒng)計(jì)情況
引入包和加載數(shù)據(jù)
1
2
3
4
5
import pandas as pd
import numpy as np
train_df =pd.read_csv("../datas/train.csv") # train set
test_df = pd.read_csv("../datas/test.csv") # test set
combine = [train_df, test_df]
清洗數(shù)據(jù)
查看數(shù)據(jù)維度以及類型
缺失值處理
查看object數(shù)據(jù)統(tǒng)計(jì)信息
數(shù)值屬性離散化
計(jì)算特征與target屬性之間關(guān)系
查看數(shù)據(jù)維度以及類型
1
2
3
4
5
6
print train_df.head(5)
查看每列數(shù)據(jù)類型以及nan情況print train_df.info()
獲得所有object屬性print train_data.describe(include=["O"]).columns
查看object數(shù)據(jù)統(tǒng)計(jì)信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
print train_df.describe()
查看object屬性數(shù)據(jù)統(tǒng)計(jì)情況print train_df.describe(include=["O"])
統(tǒng)計(jì)Title單列各個(gè)元素對應(yīng)的個(gè)數(shù)print train_df["Title"].value_counts()
屬性列刪除train_df = train_df.drop(["Name", "PassengerId"], axis=1)
缺失值處理
print df4.dropna(axis=0,subset=["col1"]) # 丟棄nan的行,subset指定查看哪幾列
print df4.dropna(axis=1) # 丟棄nan的列
dataset["Cabin"] = dataset["Cabin"].fillna("U")
dataset["Title"] = dataset["Title"].fillna(0)
freq_port = train_df.Embarked.dropna().mode()[0]
dataset["Embarked"] = dataset["Embarked"].fillna(freq_port)
test_df["Fare"].fillna(test_df["Fare"].dropna().median(), inplace=True)
test_df["Fare"].fillna(test_df["Fare"].dropna().mean(), inplace=True)
數(shù)值屬性離散化,object屬性數(shù)值化
train_df["FareBand"] = pd.qcut(train_df["Fare"], 4)
查看切分后的屬性與target屬性Survive的關(guān)系train_df[["FareBand", "Survived"]].groupby(["FareBand"], as_index=False).mean().sort_values(by="FareBand", ascending=True)
建立object屬性映射字典title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royalty":5, "Officer": 6}
dataset["Title"] = dataset["Title"].map(title_mapping)
計(jì)算特征與target屬性之間關(guān)系
object與連續(xù)target屬性之間,可以groupby均值
object與離散target屬性之間,先將target數(shù)值化,然后groupby均值,或者分別條形統(tǒng)計(jì)圖
連續(xù)屬性需要先切割然后再進(jìn)行g(shù)roupby計(jì)算,或者pearson相關(guān)系數(shù)
print train_df[["AgeBand", "Survived"]].groupby(["AgeBand"], as_index=False).mean().sort_values(by="AgeBand", ascending=True)
總結(jié)pandas基本操作
”"
創(chuàng)建df對象
””"
s1 = pd.Series([1,2,3,np.nan,4,5])
s2 = pd.Series([np.nan,1,2,3,4,5])
print s1
dates = pd.date_range(“20130101”,periods=6)
print dates
df = pd.DataFrame(np.random.rand(6,4),index=dates,columns=list(“ABCD”))
df2 = pd.DataFrame({“A”:1,
‘B":pd.Timestamp(‘20130102"),
‘C":pd.Series(1,index=list(range(4)),dtype="float32"),
‘D":np.array([3]*4,dtype=np.int32),
‘E":pd.Categorical([‘test","train","test","train"]),
‘F":"foo"
})
df3 = pd.DataFrame({"col1":s1,
"col2":s2
})
print df3
"""
2.查看df數(shù)據(jù)
"""
print df3.head(2) #查看頭幾條
print df3.tail(3) #查看尾幾條
print df.index #查看索引
print df.info() #查看非non數(shù)據(jù)條數(shù)
print type(df.values) #返回二元數(shù)組
print df.describe() #對每列數(shù)據(jù)進(jìn)行初步的統(tǒng)計(jì)
print df3
print df3.sort_values(by=["col1"],axis=0,ascending=True) #按照哪幾列排序
"""
3.選擇數(shù)據(jù)
"""
ser_1 = df3["col1"]
print type(ser_1) #pandas.core.series.Series
print df3[0:2] #前三行
print df3.loc[df3.index[0]] #通過index來訪問
print df3.loc[df3.index[0],["col2"]] #通過行index,和列名來唯一確定一個(gè)位置
print df3.iloc[1] #通過位置來訪問
print df3.iloc[[1,2],1:2] #通過位置來訪問
print "==="
print df3.loc[:,["col1","col2"]].as_matrix() # 返回nunpy二元數(shù)組
print type(df3.loc[:,["col1","col2"]].as_matrix())
"""
4.布爾索引,過濾數(shù)據(jù)
"""
print df3[df3.col1 >2]
df4 = df3.copy()
df4["col3"]=pd.Series(["one","two","two","three","one","two"])
print df4
print df4[df4["col3"].isin(["one","two"])]
df4.loc[:,"col3"]="five"
print df4
"""
5.缺失值處理,pandas將缺失值用nan代替
"""
print pd.isnull(df4)
print df4.dropna(axis=0,subset=["col1"]) # 丟棄nan的行,subset指定查看哪幾列
print df4.dropna(axis=1) # 丟棄nan的列
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/43330.html
摘要:在這里我分享下我個(gè)人入門機(jī)器學(xué)習(xí)的經(jīng)歷,希望能對大家能有所幫助。相關(guān)學(xué)習(xí)鏈接,,入門后的體驗(yàn)在入門了機(jī)器學(xué)習(xí)之后,在實(shí)際工作中,絕大多數(shù)的情況下你并不需要去創(chuàng)造一個(gè)新的算法。 機(jī)器學(xué)習(xí)在很多眼里就是香餑餑,因?yàn)闄C(jī)器學(xué)習(xí)相關(guān)的崗位在當(dāng)前市場待遇不錯(cuò),但同時(shí)機(jī)器學(xué)習(xí)在很多人面前又是一座大山,因?yàn)榘l(fā)現(xiàn)它太難學(xué)了。在這里我分享下我個(gè)人入門機(jī)器學(xué)習(xí)的經(jīng)歷,希望能對大家能有所幫助。 PS:這篇文章...
Python Pandas的主要左右是解決大量的數(shù)據(jù),快速的對數(shù)據(jù)去進(jìn)行批量的處理,大大提高工作的效率。那么,里面的loc和iloc函數(shù),具體是怎么進(jìn)行使用呢?怎么知道每個(gè)函數(shù)的基本用法呢?下面小編就給大家詳細(xì)的解答下。 1 loc和iloc的含義 loc表示location的意思;iloc中的loc意思相同,前面的i表示integer,所以它只接受整數(shù)作為參數(shù)。 2用法 import...
閱讀 1202·2021-11-24 09:38
閱讀 2608·2021-09-27 14:00
閱讀 1166·2019-08-30 15:55
閱讀 1344·2019-08-30 14:16
閱讀 1495·2019-08-30 10:54
閱讀 2869·2019-08-28 17:58
閱讀 762·2019-08-26 13:22
閱讀 1238·2019-08-26 12:01