pandas數(shù)據(jù)清洗實(shí)現(xiàn)刪除的項(xiàng)目實(shí)踐
準(zhǔn)備工作(導(dǎo)入庫(kù)、導(dǎo)入數(shù)據(jù))
import pandas as pd import matplotlib.pyplot as plt import numpy as np import seaborn as ?sns sns.set_style("darkgrid") ??
list_csv = ['Amazon_top_selling_book.csv','breast_cancer_wisconsin.csv','diamonds.csv','insurance.csv','netflix_titles.csv','penguins.csv', 'titanic.csv','winequality-red.csv'] dic_path = r'C:\Users\pandas\Desktop\task\228datasets\datasets' part_data = pd.read_csv(dic_path+'\\'+list_csv[4]) part_data
show_id | type | title | director | cast | country | date_added | release_year | rating | duration | listed_in | description | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | s1 | Movie | Dick Johnson Is Dead | Kirsten Johnson | NaN | United States | September 25, 2021 | 2020 | PG-13 | 90 min | Documentaries | As her father nears the end of his life, filmm... |
1 | s2 | TV Show | Blood & Water | NaN | Ama Qamata, Khosi Ngema, Gail Mabalane, Thaban... | South Africa | September 24, 2021 | 2021 | TV-MA | 2 Seasons | International TV Shows, TV Dramas, TV Mysteries | After crossing paths at a party, a Cape Town t... |
2 | s3 | TV Show | Ganglands | Julien Leclercq | Sami Bouajila, Tracy Gotoas, Samuel Jouy, Nabi... | NaN | September 24, 2021 | 2021 | TV-MA | 1 Season | Crime TV Shows, International TV Shows, TV Act... | To protect his family from a powerful drug lor... |
3 | s4 | TV Show | Jailbirds New Orleans | NaN | NaN | NaN | September 24, 2021 | 2021 | TV-MA | 1 Season | Docuseries, Reality TV | Feuds, flirtations and toilet talk go down amo... |
4 | s5 | TV Show | Kota Factory | NaN | Mayur More, Jitendra Kumar, Ranjan Raj, Alam K... | India | September 24, 2021 | 2021 | TV-MA | 2 Seasons | International TV Shows, Romantic TV Shows, TV ... | In a city of coaching centers known to train I... |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
8807 rows × 12 columns
檢測(cè)數(shù)據(jù)情況
Hint:該函數(shù)用于檢測(cè)任意DataFrame中缺失值情況
def missing_values_table(df): mis_val = df.isnull().sum() mis_val_percent = 100 * df.isnull().sum() / len(df) mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1) mis_val_table_ren_columns = mis_val_table.rename( columns = {0 : 'Missing Values', 1 : '% of Total Values'}) mis_val_table_ren_columns = mis_val_table_ren_columns[ mis_val_table_ren_columns.iloc[:,1] != 0].sort_values( '% of Total Values', ascending=False).round(1) print ("Your selected dataframe has " + str(df.shape[1]) + " columns.\n" "There are " + str(mis_val_table_ren_columns.shape[0]) + " columns that have missing values.") return mis_val_table_ren_columns
missing_values_table(part_data)
Your selected dataframe has 12 columns.
There are 6 columns that have missing values.
Missing Values | % of Total Values | |
---|---|---|
director | 2634 | 29.9 |
country | 831 | 9.4 |
cast | 825 | 9.4 |
date_added | 10 | 0.1 |
rating | 4 | 0.0 |
duration | 3 | 0.0 |
DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False)
參數(shù)說(shuō)明:
- labels 就是要?jiǎng)h除的行列的名字,用列表給定
- axis 默認(rèn)為0,指刪除行,因此刪除columns時(shí)要指定axis=1;
- index 直接指定要?jiǎng)h除的行
- columns 直接指定要?jiǎng)h除的列
- inplace=False,默認(rèn)該刪除操作不改變?cè)瓟?shù)據(jù),而是返回一個(gè)執(zhí)行刪除操作后的新dataframe;
- inplace=True,則會(huì)直接在原數(shù)據(jù)上進(jìn)行刪除操作,刪除后無(wú)法返回。
方式一:刪除指定行或列
labels+axis
demo = part_data.drop(['director'], axis=1) missing_values_table(demo)
Your selected dataframe has 11 columns.
There are 5 columns that have missing values.
Missing Values | % of Total Values | |
---|---|---|
country | 831 | 9.4 |
cast | 825 | 9.4 |
date_added | 10 | 0.1 |
rating | 4 | 0.0 |
duration | 3 | 0.0 |
方式二:利用boolean刪除滿足條件元素所在的行
df = df.drop(df[].index)
# 刪除release_year年份在2009年之前的行 demo = part_data.drop(part_data[part_data["release_year"]<2009].index) demo.shape
(7624, 12)
到此這篇關(guān)于pandas數(shù)據(jù)清洗實(shí)現(xiàn)刪除的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)pandas數(shù)據(jù)清洗刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Pandas 數(shù)據(jù)處理,數(shù)據(jù)清洗詳解
- 利用pandas進(jìn)行數(shù)據(jù)清洗的方法
- Pandas數(shù)據(jù)清洗函數(shù)總結(jié)
- Python數(shù)據(jù)清洗之利用pandas篩選數(shù)據(jù)詳解
- Pandas數(shù)據(jù)清洗的實(shí)現(xiàn)
- 利用pandas進(jìn)行數(shù)據(jù)清洗的7種方式
- 基于pandas數(shù)據(jù)清洗的實(shí)現(xiàn)示例
- Pandas數(shù)據(jù)清洗與過(guò)濾空值技巧
- Pandas數(shù)據(jù)清洗的維度詳解
- Pandas 數(shù)據(jù)清洗的具體使用
相關(guān)文章
Python使用pickle模塊存儲(chǔ)數(shù)據(jù)報(bào)錯(cuò)解決示例代碼
這篇文章主要介紹了Python使用pickle模塊存儲(chǔ)數(shù)據(jù)報(bào)錯(cuò)解決示例代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python實(shí)現(xiàn)Wordcloud生成詞云圖的示例
這篇文章主要介紹了Python實(shí)現(xiàn)Wordcloud生成詞云圖的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03解決Python訪問(wèn)MySQL數(shù)據(jù)庫(kù)速度慢的問(wèn)題
這篇文章主要介紹了解決Python訪問(wèn)MySQL數(shù)據(jù)庫(kù)速度慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04Python單元測(cè)試框架unittest使用方法講解
這篇文章主要介紹了Python單元測(cè)試框架unittest使用方法講解,本文講解了unittest概述、命令行接口、測(cè)試案例自動(dòng)搜索、創(chuàng)建測(cè)試代碼、構(gòu)建測(cè)試套件方法等內(nèi)容,需要的朋友可以參考下2015-04-04python GUI庫(kù)圖形界面開(kāi)發(fā)之pyinstaller打包python程序?yàn)閑xe安裝文件
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之pyinstaller打包python程序?yàn)閑xe安裝文件,需要的朋友可以參考下2020-02-02python算法與數(shù)據(jù)結(jié)構(gòu)朋友圈與水杯實(shí)驗(yàn)題分析實(shí)例
這篇文章主要介紹了python算法與數(shù)據(jù)結(jié)構(gòu)朋友圈與水杯實(shí)驗(yàn)題分析,總的來(lái)說(shuō)這并不是難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過(guò)程。希望通過(guò)這道題能給你帶來(lái)一種解題優(yōu)化的思路2022-12-12python 設(shè)置文件編碼格式的實(shí)現(xiàn)方法
下面小編就為大家分享一篇python 設(shè)置文件編碼格式的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12