首页 > 解决方案 > 从 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.whereor regexthen之前删除单词apply

标签: pythonpandas

解决方案


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

推荐阅读