亲宝软件园·资讯

展开

pandas数据清洗删除

ACxz 人气:0

准备工作(导入库、导入数据)

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_idtypetitledirectorcastcountrydate_addedrelease_yearratingdurationlisted_indescription
0s1MovieDick Johnson Is DeadKirsten JohnsonNaNUnited StatesSeptember 25, 20212020PG-1390 minDocumentariesAs her father nears the end of his life, filmm...
1s2TV ShowBlood & WaterNaNAma Qamata,
Khosi Ngema,
Gail Mabalane,
Thaban...
South AfricaSeptember 24, 20212021TV-MA2 SeasonsInternational TV Shows,
TV Dramas,
TV Mysteries
After crossing paths at a party, a Cape Town t...
2s3TV ShowGanglandsJulien LeclercqSami Bouajila,
Tracy Gotoas,
Samuel Jouy,
Nabi...
NaNSeptember 24, 20212021TV-MA1 SeasonCrime TV Shows,
International TV Shows,
TV Act...
To protect his family from a powerful drug lor...
3s4TV ShowJailbirds New OrleansNaNNaNNaNSeptember 24, 20212021TV-MA1 SeasonDocuseries, Reality TVFeuds, flirtations and toilet talk go down amo...
4s5TV ShowKota FactoryNaNMayur More,
Jitendra Kumar,
Ranjan Raj,
Alam K...
IndiaSeptember 24, 20212021TV-MA2 SeasonsInternational 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
director263429.9
country8319.4
cast8259.4
date_added100.1
rating40.0
duration30.0

DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False)

参数说明:

方式一:删除指定行或列

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
country8319.4
cast8259.4
date_added100.1
rating40.0
duration30.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数据清洗实现删除的项目实践的文章就介绍到这了,更多相关pandas数据清洗删除内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

加载全部内容

相关教程
猜你喜欢
用户评论