这篇文章主要介绍“pandas数据清洗如何实现删除”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“pandas数据清洗如何实现删除”文章能帮助大家解决问题。
准备工作(导入库、导入数据)
import pandas as pdimport matplotlib.pyplot as pltimport numpy as npimport seaborn as snssns.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
检测数据情况
Hint:该函数用于检测任意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)
参数说明:
labels 就是要删除的行列的名字,用列表给定
axis 默认为0,指删除行,因此删除columns时要指定axis=1;
index 直接指定要删除的行
columns 直接指定要删除的列
inplace=False,默认该删除操作不改变原数据,而是返回一个执行删除操作后的新dataframe;
inplace=True,则会直接在原数据上进行删除操作,删除后无法返回。
方式一:删除指定行或列
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)
关于“pandas数据清洗如何实现删除”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。