python - 从 pandas 的行中删除特定模式
问题描述
我试图弄清楚如何计算以数字开头的行,例如:
My_col
24 was 2020 - There is a lot -
23 aka 2018 - how many ...
23 was 2020 - wonderful!
no numbers this time
并且,仅当以数字开头时,删除 before 三个单词之前的单词-
:
My_col
There is a lot -
how many ...
wonderful!
no numbers this time
使用 SQL 我会做如下检查:
SELECT CASE WHEN ISNUMERIC(SUBSTRING(LTRIM(My_Col), 1, 1)) = 1
THEN 'yes'
ELSE 'no'
END AS StartsWithNumber
FROM my_data
-
我认为在我应该考虑使用np.where
or regex
then之前删除单词apply
。
解决方案
df = pd.DataFrame({'My_col': [
"24 was 2020 - There is a lot -",
"no numbers this time"] })
df['My_col'].apply(
lambda x: x[x.find("-")+1:].strip() if x[0].isdigit() else x)
输出:
0 There is a lot -
1 no numbers this time
推荐阅读
- python - Pandas 跟踪 .0 问题
- javascript - 如何正确使用工具类更改 div 的 css?
- angular - Angular CLI - 无法使用自定义环境文件进行测试
- mysql - 从 MySQL 中的 JSON 列中的 JSON 项目的日期查询日期之前的记录
- visual-studio - 你如何从 Visual Studio 运行 cpack?
- javascript - 在 json 中查找并在另一个 json 中设置
- python - 使用未声明的变量
- hikvision - 在海康威视相机集成中出现错误 23
- unity3d - Oculus Go with Unity:您现在可以附加调试器
- c++ - SFML 中的字体。信息不显示在屏幕上。显示的不是信息,而是一个点