import numpy as np
import pandas as pd
读取"豆瓣电影数据.xlsx"文件进行分析清洗
df1 = pd.read_excel(‘豆瓣电影数据.xlsx‘, index_col=0)
df1.head()
方法 | 说明 |
---|---|
dropna | 根据标签中缺失值进行过滤,删除缺失值 |
fillna | 对缺失值进行填充 |
isnull | 返回一个布尔值对象,判断哪些值是缺失值 |
notnull | isnull的否定值 |
df1.info()
df1.describe()
1. 查看名字中有缺失值的行数据
df1[df1.名字.isnull()].head()
tmp1 = df1[df1.名字.isnull()].index
tmp1
df1.drop(tmp1,inplace=True)
df1[df1.名字.isnull()]
1. 投票人数小于0和投票人数为小数的数据
idx1 = df1[(df1.投票人数 < 0) | (df1.投票人数 % 1 != 0)].index
df1.drop(idx1, inplace=True)
2.验证清理情况
df1[(df1.投票人数 < 0) | (df1.投票人数 % 1 != 0)]
df1.head()
df1.iloc[3, 7] = 9.4
df1.head()
1. 查看投票人数列的格式
df1.投票人数.dtype
df1.投票人数 = df1.投票人数.astype(np.int32)
df1.head()
2.查看年代列的格式
df1.年代.dtype
df1.年代.astype(np.int32)
df1[df1.年代 == ‘2008\u200e‘]
df1.loc[15205, "年代"] = 2008
df1["年代"] = df1.年代.astype(np.int32)
df1.年代.dtype
df1.describe()
3. 查看时长列格式
df1.时长.dtype
df1.时长.astype(np.int32)
df1[df1.时长 == ‘8U‘]
df1.drop([31644], inplace=True)
df1.时长.astype(np.int32)
df1[df1.时长==‘12J‘]
df1.drop([32949], inplace=True)
df1["时长"] = df1.时长.astype(np.int32)
df1.head()
df1.describe()
df1.info()
df1[df1.年代 > 2019]
df1[df1.时长 > 1000]
df1.drop(df1[df1.年代>2019].index, inplace=True)
df1.drop(df1[df1.时长>1000].index, inplace=True)
df1.describe()
df1[230:235]
df1.index = range(len(df1))
df1[230:235]
df1.产地.unique()
df1.产地.replace("USA", "美国", inplace=True)
df1.产地.replace(["USA" ,"西德", "苏联"], ["美国", "德国", "俄罗斯"], inplace=True)
df1.产地.unique()
df1.to_excel(‘movie.xlsx‘)
原文:https://www.cnblogs.com/sunblingbling/p/12393012.html