python - 删除所有满足正则表达式条件的行
问题描述
试图自学熊猫..并玩弄不同的dtypes
我有一个df如下
df = pd.DataFrame({'ID':[0,2,"bike","cake"], 'Course':['Test','Math','Store','History'] })
print(df)
ID Course
0 0 Test
1 2 Math
2 bike Store
3 cake History
ID 的 dtype 当然是一个对象。如果 ID 中有一个字符串,我想要做的是删除 DF 中的任何行。
我以为这很简单..
df.ID.filter(regex='[\w]*')
但这会返回所有内容,是否有可靠的方法来处理此类事情?
解决方案
你可以使用to_numeric
df[pd.to_numeric(df.ID,errors='coerce').notnull()]
Out[450]:
Course ID
0 Test 0
1 Math 2
推荐阅读
- java - 在用户退出应用程序之前找到最后一个 onBackPressed
- ssis - 如何在 SSMS 中将 PackagePassword 传递给 SSIS 包的执行
- python - Python for loop TRUE 或 FALSE 停止处理
- php - 如何更改 phpspreadsheet html 阅读器中的列属性
- google-bigquery - 错误:GoogleJsonResponseException:对 bigquery.jobs.get 的 API 调用失败并出现错误:未找到:作业 YXZ
- python - Quandl For Loop NotFoundError Python
- reactjs - REACT NATIVE - 如何根据最长的文本对齐列表中的项目?
- swift - 通过tableview单元格中的按钮将图像导入imageview
- python - 带有 Python 3.8 的 pyzbar 无法检测扫描文档中的二维码
- c++ - 这个自动模板扣除指南是否正确?