python - 如何找到所有()一个 Pandas 数据框的正则表达式序列?

标签 python python-3.x pandas

我正在用 pandas 提取一些模式 findall功能。但是,我有几个正则表达式。这个,我如何用 pandas findall N 正则表达式?。

例如,假设我想提取特定列中的所有数字和所有日期:

在:

dfs = pd.DataFrame(data={'c1': ['This dataset 11/12/98 contains 5,000 rows, which were sampled from a 500,000 11/12/12 row dataset spanning the same time period. Throughout these analyses', 

                                'the number of events you count will be about 100 times smaller than they 11/12/78 actually were, but the 01/12/11 proportions of events will still generally be reflective that larger dataset. In this case, a sample is fine because our purpose is to learn methods of data analysis with Python, not to create 100% accurate recommendations to Watsi.']})

dfs

输出:

    c1
0   This dataset 11/12/98 contains 5,000 rows, whi...
1   the number of events you count will be about 1...

我试过,但出现以下错误:

在:

dfs['patterns'] = dfs['c1'].str.findall([r'\d+',r'(\d+/\d+/\d+)']).apply(', '.join)

dfs

输出:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-64-af2969e06a61> in <module>()
----> 1 dfs['patterns'] = dfs['c1'].str.findall([r'\d+',r'(\d+/\d+/\d+)']).apply(', '.join)
      2 dfs

/usr/local/lib/python3.5/site-packages/pandas/core/strings.py in wrapper2(self, pat, flags, **kwargs)
   1268 
   1269     def wrapper2(self, pat, flags=0, **kwargs):
-> 1270         result = f(self._data, pat, flags=flags, **kwargs)
   1271         return self._wrap_result(result)
   1272 

/usr/local/lib/python3.5/site-packages/pandas/core/strings.py in str_findall(arr, pat, flags)
    827     extractall : returns DataFrame with one column per capture group
    828     """
--> 829     regex = re.compile(pat, flags=flags)
    830     return _na_map(regex.findall, arr)
    831 

/usr/local/Cellar/python3/3.5.2_2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/re.py in compile(pattern, flags)
    222 def compile(pattern, flags=0):
    223     "Compile a regular expression pattern, returning a pattern object."
--> 224     return _compile(pattern, flags)
    225 
    226 def purge():

/usr/local/Cellar/python3/3.5.2_2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/re.py in _compile(pattern, flags)
    279     # internal: compile pattern
    280     try:
--> 281         p, loc = _cache[type(pattern), pattern, flags]
    282         if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
    283             return p

TypeError: unhashable type: 'list'

因此,如何使用 findall 函数“堆叠”、“嵌套”或应用多个正则表达式?。 我期望的输出是单个列中由 , 分隔的每个正则表达式的解析:

   col
0  '11/12/98', '5', '000', '500', '000', '11/12/12'
1  '100', '11/12/78', '01/12/11', '100'

更新

我尝试过:

dfs['patterns'] = dfs['c1'].str.map(findall(),[r'\d+',r'(\d+/\d+/\d+)']).apply(', '.join)
dfs

最佳答案

仍未清除您想要的输出。 但是请检查下面的代码。

dfs['patterns'] = dfs['c1'].str.findall(r'\d+\/\d+\/\d+|\d+')
print dfs['patterns'].sum()

['11/12/98', '5', '000', '500', '000', '11/12/12', '100', '11/12/78', '01/12/11', '100']

关于python - 如何找到所有()一个 Pandas 数据框的正则表达式序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290076/

相关文章:

python-3.x - 使用 python filename.py 执行时没有名为 psutil 的模块,但与 shebang & chmod 完美配合

python - 如何使用 python 将数据框附加到基于 header 的现有 Excel 文件

python - 使用 Pandas 从 xml url 读取单个节点

python - 使用 pandas.dataframe.query method() 将数据框替换为子集的适当方法是什么?

python - 如何在python中读取json对象

python - numpy 标准差给出的结果与 scipy stats 标准差不同

python - 为什么列表乘法这么快?

python-3.x - brew install 没有链接python3

python - 拆分名字和姓氏正则表达式

python - 如何禁用 celery worker 的 worker 日志输出?