Python pandas 具有 lambda 应用难度

Python pandas with lambda apply difficulty

我是 运行 以下函数,但不知何故努力让它考虑长度条件(if 部分)。如果只有函数,它只运行第一部分:

stringDataFrame.apply(lambda x: x.str.replace(r'[^0-9]', '') if (len(x) >= 7) else x)

出于某种原因,它以某种方式只运行 x.str.replace(r'[^0-9]', '') 部分,我在这里做错了什么我被卡住了。

当您需要分别处理每个值时,您可以使用 applymap,因为 applyall column (Series) 一起使用。

然后不使用 str.replace, use re.sub 这对正则表达式更有效:

print (stringDataFrame.applymap(lambda x: re.sub(r'[^0-9]', '', x) if (len(x) >= 7) else x))

样本:

import pandas as pd
import re

stringDataFrame = pd.DataFrame({'A':['gdgdg454dgd','147ooo2', '123ss45678'],
                                'B':['gdgdg454dgd','x142', '12345678a'],
                                'C':['gdgdg454dgd','xx142', '12567dd8']})

print (stringDataFrame)
             A            B            C
0  gdgdg454dgd  gdgdg454dgd  gdgdg454dgd
1      147ooo2         x142        xx142
2   123ss45678    12345678a     12567dd8

print (stringDataFrame.applymap(lambda x: re.sub(r'[^0-9]', '', x) if (len(x) >= 7) else x))
          A         B       C
0       454       454     454
1      1472      x142   xx142
2  12345678  12345678  125678