Home>Article>Backend Development> 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
# 导入 pandas 库import pandas as pd# read_excel() 读取 excel 数据# DataFrame() 将读取到的数据转换为 DataFrame 数据df = pd.DataFrame(pd.read_excel('data.xlsx'))
# dropna() 函数去除 df 数据表中存在空值的所有行df.dropna(how='any')# mean() 函数计算 age 字段所在列的平均值age_pre = df['age'].mean()# 使用 fillna() 函数对存在的空值进行填充,将 age_pre 的值填充到字段为空的值内面df['age'].fillna(age_pre)
# 清除字段的空格df['name'] = df['name'].map(str.strip)
# rename() 函数对列进行重命名df.rename(columns={'name': 'name_new'})
# 从前往后查找某个列中的重复值,如果存在则清除后面所出现的重复值df['name'].drop_duplicates()# 从后往前查找某个列中的重复值,如果存在则清除前面所出现的重复值df['city'].drop_duplicates(keep='last')# 两种正好是按照相反的清除顺序
# 将某一列中的具体值进行替换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!