python - 检查数据集中的奇怪项目
问题描述
我想检查 Python 中行数较少的数据框中的奇怪分类项目
我尝试使用以下代码来显示奇怪的项目。
for i in range(data.shape[1]):
if data[data.columns[i]].dtype == "object":
print(data[data.columns[i]].value_counts())
是否有任何方法使用更少的行来检查分类数据?
解决方案
如果要打印列的所有唯一条目,我建议使用unique
( docs ) 方法
>>> a = pd.DataFrame({'sex':['m','f','m','m','m', 'f', 'booooy']})
>>> a.loc[:,'sex'].unique()
Out[1]: array(['m', 'f', 'booooy'], dtype=object)
要将booooy
条目更改为m
您可以使用re.sub
( docs ) 方法
>>> a.loc[:,'sex'].apply(lambda x: re.sub(r'booooy','m', x))
Out[2]:
0 m
1 f
2 m
3 m
4 m
5 f
6 m
Name: sex, dtype: object
如果您有很多re.sub
电话-您可以将它们放入一个函数中,而不是应用它们
>>> def filter_text(x):
... x = re.sub(r'booooy','m',x)
... x = re.sub(r'girl','f',x)
... # . . . . . .
... return x
>>> a.loc[:,'sex'].apply(filter_text)
Out[3]:
0 m
1 f
2 m
3 m
4 m
5 f
6 m
Name: sex, dtype: object
希望有帮助!
推荐阅读
- html - 将页脚固定到页面底部
- c# - 如何在静态无效中调用元素?
- javascript - 为什么表单上的“提交”按钮会不断刷新页面,即使表单调用的函数末尾存在“return false”?
- python - 标签 h4 包含在 div 类中
- qt - 如何在 QCustomPlot 中开启 OpenGL 加速?
- jquery - Symfony fullcalendar 按 id 删除事件
- javascript - 如何在浏览器的 JavaScript 中检查服务器是否支持 HTTP/2
- r - 结合反应性和事件反应性来生成绘图
- javascript - 当我尝试在 reactjs 中加载 css 时出现错误
- java - IntelliJ 调试 - 监视包含特定值的任何变量