Home > Backend Development > Python Tutorial > python operation excel series: data cleaning

python operation excel series: data cleaning

coldplay.xixi
Release: 2021-03-02 10:20:00
forward
4063 people have browsed it

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'))
Copy after login
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)
Copy after login
3, data cleaning (clear spaces in fields)
# 清除字段的空格df['name'] = df['name'].map(str.strip)
Copy after login
4, data cleaning (remove a certain Column rename)
# rename() 函数对列进行重命名df.rename(columns={'name': 'name_new'})
Copy after login
5, Data cleaning (removing duplicate values ​​in a column)
# 从前往后查找某个列中的重复值,如果存在则清除后面所出现的重复值df['name'].drop_duplicates()# 从后往前查找某个列中的重复值,如果存在则清除前面所出现的重复值df['city'].drop_duplicates(keep='last')# 两种正好是按照相反的清除顺序
Copy after login
6, Data cleaning (data value replacement)
# 将某一列中的具体值进行替换df['name'].replace('laow', 'lwsbc')
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template