Home>Article>Backend Development> python operation excel series: data cleaning

python operation excel series: data cleaning

coldplay.xixi
coldplay.xixi forward
2021-03-02 10:20:00 3867browse

python operation excel series: data cleaning

# While operating excel, the previous article talked about reading, inserting, and simple analysis of data. Another very important point is data cleaning. So what is data cleaning? To put it bluntly, it means removing junk values in data text, such as: existing null values, redundant spaces, data formats, etc.

Related free learning recommendations:python video tutorial

1, import the python library and read excel Data
# 导入 pandas 库import pandas as pd# read_excel() 读取 excel 数据# DataFrame() 将读取到的数据转换为 DataFrame 数据df = pd.DataFrame(pd.read_excel('data.xlsx'))
2, data cleaning (remove null values)
# dropna() 函数去除 df 数据表中存在空值的所有行df.dropna(how='any')# mean() 函数计算 age 字段所在列的平均值age_pre = df['age'].mean()# 使用 fillna() 函数对存在的空值进行填充,将 age_pre 的值填充到字段为空的值内面df['age'].fillna(age_pre)
3, data cleaning (clear spaces in fields)
# 清除字段的空格df['name'] = df['name'].map(str.strip)
4, data cleaning (remove a certain Column rename)
# rename() 函数对列进行重命名df.rename(columns={'name': 'name_new'})
5, Data cleaning (removing duplicate values in a column)
# 从前往后查找某个列中的重复值,如果存在则清除后面所出现的重复值df['name'].drop_duplicates()# 从后往前查找某个列中的重复值,如果存在则清除前面所出现的重复值df['city'].drop_duplicates(keep='last')# 两种正好是按照相反的清除顺序
6, Data cleaning (data value replacement)
# 将某一列中的具体值进行替换df['name'].replace('laow', 'lwsbc')

Related free learning recommendations:python tutorial(Video)

The above is the detailed content of python operation excel series: data cleaning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete