Pandas根据另一列的子字符串中的数字从子字符串切片创建新列

时间:2016-06-18 12:46:11

标签: python numpy pandas dataframe

我有一个名为' table'的数据框。像这样:

import pandas as pd
import numpy as np
table = pd.read_csv(main_data, sep='\t')

它产生了这个:

NAME    SYMBOL    STRING
A       blah       A34SA
B       foo        BS2812D
...

如何在pandas中创建新列,以便我有以下内容:

NAME     SYMBOL      STRING    NUMBER
   A       blah       A34SA        34 
   B        foo     BS2812D      2812

到目前为止我有这个: table['NUMBER'] = table.STRING.str[int(filter(str.isdigit, table.STRING))]但此功能在此上下文中不起作用。

谢谢!

4 个答案:

答案 0 :(得分:2)

您可以尝试使用正则表达式从字符串中提取数字:

import re
def extNumber(row):
    row['NUMBER'] = re.search("(\\d+)", row.STRING).group(1)
    return row

df.apply(extNumber, axis=1)

答案 1 :(得分:2)

以下内容应该有效

table['NUMBER'] = table.STRING.apply(lambda x: int(''.join(filter(str.isdigit, x))))

答案 2 :(得分:2)

您可以使用正则表达式。

import re

table['NUMBER'] = table['STRING'].apply(lambda x: re.sub(r'[^0-9]','',x))

答案 3 :(得分:2)

我会这样做:

In [22]: df['NUMBER'] = df.STRING.str.extract('(?P<NUMBER>\d+)', expand=True).astype(int)

In [23]: df
Out[23]:
  NAME SYMBOL   STRING  NUMBER
0    A   blah    A34SA      34
1    B    foo  BS2812D    2812

In [24]: df.dtypes
Out[24]:
NAME      object
SYMBOL    object
STRING    object
NUMBER     int32
dtype: object

时间针对20M行DF:

In [71]: df = pd.concat([df] * 10**7, ignore_index=True)

In [72]: df.shape
Out[72]: (20000000, 3)

In [73]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20000000 entries, 0 to 19999999
Data columns (total 3 columns):
NAME      object
SYMBOL    object
STRING    object
dtypes: object(3)
memory usage: 457.8+ MB

In [74]: %timeit df.STRING.str.replace(r'\D+', '').astype(int)
1 loop, best of 3: 507 ms per loop

In [75]: %timeit df.STRING.str.extract('(?P<NUMBER>\d+)', expand=True).astype(int)
1 loop, best of 3: 434 ms per loop

In [76]: %timeit df.STRING.apply(lambda x: int(''.join(filter(str.isdigit, x))))
1 loop, best of 3: 562 ms per loop

In [77]: %timeit df['STRING'].apply(lambda x: re.sub(r'[^0-9]','',x))
1 loop, best of 3: 552 ms per loop