小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

python-13-pandas的常用操作

 招財(cái)進(jìn)寶2011 2021-02-16

皮皮冰燃 2020-10-22 17:49:19  56  收藏

分類(lèi)專(zhuān)欄: python3 文章標(biāo)簽: python

(1)獲取數(shù)據(jù)
(2)數(shù)據(jù)處理:處理缺失數(shù)據(jù),可視化
(3)特征工程
(4)算法訓(xùn)練:模型
(5)模型評(píng)估
(6)應(yīng)用
from numpy import nan as NA

1 分析數(shù)據(jù)

df[“method”]獲取指定列
df.iloc[0]獲取第一行的數(shù)據(jù)
df.iloc[0][“code”]獲取第一行指定列的數(shù)據(jù)
df.shape()獲取行數(shù)和列數(shù)

1 濾除缺失數(shù)據(jù)

SeriesNew = Series.dropna()SeriesNew = Series[Series.notnull()]DataFrameNew = DataFrame.dropna()刪除含NA的行
DataFrameNew = DataFrame.dropna(how="all")刪除全為NA的行
DataFrameNew = DataFrame.dropna(axis=1,how="all")刪除全為NA的列
DataFrameNew = DataFrame.dropna(axis=1)刪除含NA的列
DataFrameNew = DataFrame.dropna(thresh=n)保留至少有n個(gè)非NA的行1234567

注意:(1)DataFrame.dropna()這個(gè)操作將刪除任何至少有一個(gè)空值的行,但是它將返回一個(gè)新的DataFrame,而不更改原來(lái)的數(shù)據(jù),更改原數(shù)據(jù)可以在這個(gè)方法中指定inplace=True。

2 填充缺失數(shù)據(jù)

DataFrameNew = DataFrame.fillna(num)DataFrameNew = DataFrame.fillna({1:0.5,2:7.3})填充指定列
DataFrame.fillna(0,inplace=True)就地修改
SeriesNew = Series.fillna(Series.mean())傳入Series的平均值1234

3 移出重復(fù)數(shù)據(jù)

Series = DataFrame.duplicated()布爾型Series,各行是否是重復(fù)行
DataFrameNew = DataFrame.drop_duplicates()DataFrameNew = DataFrame.drop_duplicates(['k1'])根據(jù)k1列過(guò)濾重復(fù)項(xiàng)
DataFrameNew = DataFrame.drop_duplicates(['k1','k2'])保留最后一個(gè)
SeriesNew = Series.map(字典)SeriesNew = Series.str.lower()大寫(xiě)轉(zhuǎn)小寫(xiě)
SeriesNew = Series.map(lambda x:f(x))1234567

4 替換值

SeriesNew = Series.replace(-999,np.nan)
Series.replace(-999,np.nan,inplace=True)就地修改
SeriesNew = Series.replace([-999,-1000],np.nan)
SeriesNew = Series.replace([-999,-1000],[np.nan,0])
SeriesNew = Series.replace({-999:np.nan,-1000:0})

5 重命名軸索引

DataFrame.index = DataFrame.index.map(函數(shù))就地修改
DataFrameNew = DataFrame.rename()
s.value_counts()計(jì)數(shù)

6 圖

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
fig,axes = plt.subplot(2,2,sharex=True,sharey=True)
axes[0,0][1,0]
axes[0,1][1,1]
plt.subplots_adjust(wspace=0,hspace=0)調(diào)整子圖間距
ax.plot(x,y,‘g–’)顏色線(xiàn)型
ax.plot(x,y,linstyle=’–’,color=‘g’)顏色線(xiàn)型

ax.set_xticks([0,250,500])刻度位置
ax.set_xticklables([“one”,“two”,“three”])刻度標(biāo)簽

ax.set_title(“標(biāo)題”)標(biāo)題
ax.set_xlabel(“名稱(chēng)”)x軸的名稱(chēng)

ax.plot(x,y,label=‘one’)圖例配合label參數(shù)使用
ax.legend(loc=“best”)

ax.text()注解
ax.arrow()
ax.annotate()

plt.savefig()保存

Series.plot()
DataFrame.plot()
sns.barplot()柱狀圖
sns.distplot()直方圖和密度圖
sns.displot(a,bins=None,hist=True,kde=True,rug=False,fit=None)
sns.regplot()散點(diǎn)圖和回歸圖
sns.pairplot()散點(diǎn)圖矩陣,且對(duì)角線(xiàn)為直方圖或密度圖
sns.factorplot()
pd.merge()

7 分組

df.groupby(‘key’)
df.groupby([‘key1’,‘key2’])
df.groupby().size()

8 字符串

str.split(’,’)拆分成列表
str.strip()去除空白符(包括換行符)
substr in str 子串定位
str.index(’,’)找不到會(huì)拋異常
str.find(’:’)
str.count(substr)子串出現(xiàn)次數(shù)
str.replace(被替,填充)
re.split(’\s+’,str)按一個(gè)或多個(gè)空白符(制表符、空格、換行符),拆分成列表

regex = re.compile(正則表達(dá)式)
regex.findall(str)返回列表
regex.search(str)起始和結(jié)束位置
regex.sub(替換物,str)返回新的字符串

9 使用

DataFrame.query(“a>2”)對(duì)數(shù)據(jù)框進(jìn)行挑選行的操作
(1)時(shí)間戳轉(zhuǎn)換為日期時(shí)間
時(shí)間戳單位秒
pd.to_datetime()
(2)日期時(shí)間轉(zhuǎn)變?yōu)槿掌跁r(shí)間索引
pd.DatetimeIndex()
(3)是否包含在其中
Series.isin()

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多