標籤:info 資料視覺效果 white pes scribe list center ott fir
import pandas as pd #匯入pandasimport matplotlib.pyplot as plt #匯入matplotlibfrom pylab import *mpl.rcParams[‘font.sans-serif‘] = [‘SimHei‘]mpl.rcParams[‘axes.unicode_minus‘] = False%matplotlib inline
資料讀取與索引
bra = pd.read_csv(‘data/bra.csv‘)bra.head()
選取列
bra.content
bra[[‘creationTime‘,‘productColor‘]].head()
選擇行
bra[1:6]
選擇行和列
bra.ix[[2,3],[1,3]] #使用ix
bra.ix[1:5,[‘productColor‘]]
bra.iloc[[2,3],[1,3]] #使用iloc
bra.loc[1:5,[‘content‘,‘creationTime‘,‘productSize‘]] #使用loc
bra.loc[1:5,‘content‘:‘userClientShow‘]
資料預先處理缺失值
bra.describe() #查看資料的分布情況,可返回變數和觀測的數量、缺失值和唯一值的數目、平均值、分位元等相關資訊
bra[‘userClientShow‘].unique() #userClientShow列有幾種選項
bra[‘userClientShow‘].isnull().sum() #初始缺失值數量
bra[‘userClientShow‘].fillna(‘不詳‘,inplace=True) #缺失值替換為“不詳”bra[‘userClientShow‘].isnull().sum() #賦值後的缺失值數量
新增列
bra.dtypes #查看屬性
bra[‘creationTime‘] = pd.to_datetime(bra[‘creationTime‘]) #更新類型bra.dtypes
bra[‘hour‘] = [i.hour for i in bra[‘creationTime‘]] #建立hour列bra
字串操作
bra.productSize.unique() #查看productSize的唯一值
cup = bra.productSize.str.findall(‘[a-zA-Z]+‘).str[0] #新增列cupcup2 = cup.str.replace(‘M‘,‘B‘)cup3 = cup2.str.replace(‘L‘,‘C‘)cup4 = cup3.str.replace(‘XC‘,‘D‘)bra[‘cup‘] = cup4 bra.head()
bra[‘cup‘].unique() #查看cup唯一值
資料轉換
bra.productColor.unique() #查看productColor唯一值
def getColor(s): if ‘黑‘ in s: return ‘黑色‘ elif ‘膚‘ in s: return ‘膚色‘ elif ‘藍‘ in s: return ‘藍色‘ elif ‘紅‘ in s: return ‘紅色‘ elif ‘紫‘ in s: return ‘紫色‘ elif ‘白‘ in s: return ‘白色‘ elif ‘粉‘ in s: return ‘粉色‘ elif ‘灰‘ in s: return ‘灰色‘ elif ‘綠‘ in s: return ‘綠色‘ elif ‘青‘ in s: return ‘青色‘ else: return sbra[‘color‘] = bra[‘productColor‘].map(getColor) #從productColor列查詢,賦值到定義的函數getColor,最終新增列colorbra
bra.color.unique() #查詢color的唯一值
資料視覺效果
x = [1991,1992,1993,1994,1995,1996,1997]y = [23,56,38,29,34,56,92]plt.plot(x,y) #調用函數plot
plt.figure(figsize=(8,6),dpi=80) #調用函數firgureplt.plot(x,y)
hour = bra.groupby(‘hour‘)[‘hour‘].count() #hour列排序hour
plt.xlim(0,25) #橫軸0~25plt.plot(hour,linestyle=‘solid‘,color=‘royalblue‘,marker=‘8‘) #顏色深藍
cup_style = bra.groupby(‘cup‘)[‘cup‘].count() #cup列唯一值得數量cup_style
plt.figure(figsize=(8,6),dpi=80)labels = list(cup_style.index)plt.xlabel(‘cup‘) #x軸為cupplt.ylabel(‘count‘) #y軸為count數量plt.bar(range(len(labels)),cup_style,color=‘royalblue‘,alpha=0.7) #alpha為透明度plt.xticks(range(len(labels)),labels,fontsize=12)plt.grid(color=‘#95a5a6‘,linestyle=‘--‘,linewidth=1,axis=‘y‘,alpha=0.6)plt.legend([‘user-count‘])for x,y in zip(range(len(labels)),cup_style):plt.text(x,y,y,ha=‘center‘,va=‘bottom‘)
color_style = bra.groupby(‘color‘)[‘color‘].count() #color列唯一值得數量color_style
plt.figure(figsize=(8,6),dpi=80)plt.subplot(facecolor=‘gainsboro‘,alpha=0.2)colors = [‘brown‘,‘orange‘,‘gray‘,‘white‘,‘pink‘,‘purple‘,‘red‘,‘green‘,‘wheat‘,‘blue‘,‘gold‘,‘springgreen‘,‘black‘] #顏色種類labels = list(color_style.index)plt.xlabel(‘count‘) #x軸為count數量plt.ylabel(‘color‘) #y軸為colorplt.title(‘Color Distribution‘) #定義標題plt.barh(range(len(labels)),color_style,color=colors,alpha=1)plt.yticks(range(len(labels)),labels,fontsize=12)plt.grid(color=‘#95a5a6‘,linestyle=‘--‘,linewidth=1,axis=‘x‘,alpha=0.4)
bra.head(30)
Python資料分析執行個體操作