如何在pandas数据帧上计算大写和小写

时间:2018-03-12 07:42:10

标签: python string pandas text feature-extraction

这是我的数据

No  Body                 
1   DaTa Analytics 2
2   StackOver 67

这是我的预期输出

No  Body                 Uppercase   Lowercase
1   DaTa Analytics 2     3           10
2   StackOver 67         2           7

2 个答案:

答案 0 :(得分:1)

使用str.findall提取大写和小写,使用str.len获取长度:

df['Uppercase'] = df['Body'].str.findall(r'[A-Z]').str.len()
df['Lowercase'] = df['Body'].str.findall(r'[a-z]').str.len()

另一种解决方案:

df['Uppercase'] = df['Body'].str.count(r'[A-Z]')
df['Lowercase'] = df['Body'].str.count(r'[a-z]')

print (df)
   No            Body  Uppercase  Lowercase
0   1  DaTa Analytics          3         10
1   2       StackOver          2          7

答案 1 :(得分:1)

这是一个操作ASCII代码的极其高效的解决方案:

v = df.Body.values.astype(str)
v = v.view(np.uint8).reshape(len(df), -1)

df['Uppercase'] = ((v >= 65) & (v <= 90)).sum(1)
df['Lowercase'] = ((v >= 97) & (v <= 122)).sum(1)

df

   No            Body  Uppercase  Lowercase
0   1  DaTa Analytics          3         10
1   2       StackOver          2          7

<强>计时

df = pd.concat([df] * 100000, ignore_index=True)

# @jezrael1

%%timeit
df['Uppercase'] = df['Body'].str.findall(r'[A-Z]').str.len()
df['Lowercase'] = df['Body'].str.findall(r'[a-z]').str.len()

979 ms ± 24.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# @jezrael2

%%timeit
df['Uppercase'] = [sum(1 for c in x if c.isupper()) for x in df['Body']]
df['Lowercase'] = [sum(1 for c in x if c.islower()) for x in df['Body']]

1.11 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# in this post

%%timeit
v = df.Body.values.astype(str)
v = v.view(np.uint8).reshape(len(df), -1)

df['Uppercase'] = ((v >= 65) & (v <= 90)).sum(1)
df['Lowercase'] = ((v >= 97) & (v <= 122)).sum(1)

91.8 ms ± 315 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)