1.删除某列不满足长度的字符串
# 第一种方法
df=df[df['column name'].map(len) < 2]
# 第二种方法
df=df[[(len(x) < 2) for x in df['column name']]]
2.过滤其他条件
# 第一种方法
df=df.loc[df['score']>80]
# 多条件
df=df.loc[(df['score'] > 80) & (df['score'] < 100)]
# 第二种方法
df = df.drop(df[df.score < 50].index)
# inplace
df.drop(df[df.score < 50].index, inplace=True)
# 多条件
df = df.drop(df[(df.score < 50) & (df.score > 20)].index)
这篇博客介绍了如何使用Pandas库在Python中进行数据清洗。首先展示了两种方法删除列中字符串长度小于2的行,接着演示了单一条件和多条件过滤数据帧的技巧,包括利用loc和drop函数筛选满足特定条件的行。这些方法对于数据预处理和分析至关重要。
672

被折叠的 条评论
为什么被折叠?



