當前位置: 首頁>>代碼示例>>Python>>正文


Python DataFrame.name方法代碼示例

本文整理匯總了Python中pandas.core.frame.DataFrame.name方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.name方法的具體用法?Python DataFrame.name怎麽用?Python DataFrame.name使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas.core.frame.DataFrame的用法示例。


在下文中一共展示了DataFrame.name方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: str_extract

# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import name [as 別名]
def str_extract(arr, pat, flags=0):
    """
    Find groups in each string using passed regular expression

    Parameters
    ----------
    pat : string
        Pattern or regular expression
    flags : int, default 0 (no flags)
        re module flags, e.g. re.IGNORECASE

    Returns
    -------
    extracted groups : Series (one group) or DataFrame (multiple groups)


    Notes
    -----
    Compare to the string method match, which returns re.match objects.
    """
    regex = re.compile(pat, flags=flags)

    # just to be safe, check this
    if regex.groups == 0:
        raise ValueError("This pattern contains no groups to capture.")
    elif regex.groups == 1:
        def f(x):
            if not isinstance(x, compat.string_types):
                return None
            m = regex.search(x)
            if m:
                return m.groups()[0]  # may be None
            else:
                return None
    else:
        empty_row = Series(regex.groups * [None])

        def f(x):
            if not isinstance(x, compat.string_types):
                return empty_row
            m = regex.search(x)
            if m:
                return Series(list(m.groups()))  # may contain None
            else:
                return empty_row
    result = arr.apply(f)
    result.replace({None: np.nan}, inplace=True)
    if regex.groups > 1:
        result = DataFrame(result)  # Don't rely on the wrapper; name columns.
        names = dict(zip(regex.groupindex.values(), regex.groupindex.keys()))
        result.columns = [names.get(1 + i, i) for i in range(regex.groups)]
    else:
        result.name = regex.groupindex.get(0)
    return result
開發者ID:Exception4U,項目名稱:pandas,代碼行數:56,代碼來源:strings.py

示例2: str_extract

# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import name [as 別名]
def str_extract(arr, pat, flags=0):
    """
    Find groups in each string using passed regular expression

    Parameters
    ----------
    pat : string
        Pattern or regular expression
    flags : int, default 0 (no flags)
        re module flags, e.g. re.IGNORECASE

    Returns
    -------
    extracted groups : Series (one group) or DataFrame (multiple groups)

    Examples
    --------
    A pattern with one group will return a Series. Non-matches will be NaN.

    >>> Series(['a1', 'b2', 'c3']).str.extract('[ab](\d)')
    0      1
    1      2
    2    NaN
    dtype: object

    A pattern with more than one group will return a DataFrame.
    
    >>> Series(['a1', 'b2', 'c3']).str.extract('([ab])(\d)')

    A pattern may contain optional groups.
    
    >>> Series(['a1', 'b2', 'c3']).str.extract('([ab])?(\d)')

    Named groups will become column names in the result.
    
    >>> Series(['a1', 'b2', 'c3']).str.extract('(?P<letter>[ab])(?P<digit>\d)')
    """
    regex = re.compile(pat, flags=flags)

    # just to be safe, check this
    if regex.groups == 0:
        raise ValueError("This pattern contains no groups to capture.")
    elif regex.groups == 1:
        def f(x):
            if not isinstance(x, compat.string_types):
                return None
            m = regex.search(x)
            if m:
                return m.groups()[0]  # may be None
            else:
                return None
    else:
        empty_row = Series(regex.groups * [None])

        def f(x):
            if not isinstance(x, compat.string_types):
                return empty_row
            m = regex.search(x)
            if m:
                return Series(list(m.groups()))  # may contain None
            else:
                return empty_row
    result = arr.apply(f)
    result.replace({None: np.nan}, inplace=True)
    if regex.groups > 1:
        result = DataFrame(result)  # Don't rely on the wrapper; name columns.
        names = dict(zip(regex.groupindex.values(), regex.groupindex.keys()))
        result.columns = [names.get(1 + i, i) for i in range(regex.groups)]
    else:
        result.name = regex.groupindex.get(0)
    return result
開發者ID:jzwick,項目名稱:pandas,代碼行數:73,代碼來源:strings.py


注:本文中的pandas.core.frame.DataFrame.name方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。