def _create_column(data, col, value):
    """
    Create column in dataframe

    Helper method meant to deal with problematic
    column values. e.g When the series index does
    not match that of the data.

    Parameters
    ----------
    data : pandas.DataFrame
        dataframe in which to insert value
    col : column label
        Column name
    value : object
        Value to assign to column

    Returns
    -------
    data : pandas.DataFrame
        Modified original dataframe

    >>> df = pd.DataFrame({'x': [1, 2, 3]})
    >>> y = pd.Series([11, 12, 13], index=[21, 22, 23])

    Data index and value index do not match

    >>> _create_column(df, 'y', y)
       x   y
    0  1  11
    1  2  12
    2  3  13

    Non-empty dataframe, scalar value

    >>> _create_column(df, 'z', 3)
       x   y  z
    0  1  11  3
    1  2  12  3
    2  3  13  3

    Empty dataframe, scalar value

    >>> df = pd.DataFrame()
    >>> _create_column(df, 'w', 3)
       w
    0  3
    >>> _create_column(df, 'z', 'abc')
       w    z
    0  3  abc
    """
    with suppress(AttributeError):
        # If the index of a series and the dataframe
        # in which the series will be assigned to a
        # column do not match, missing values/NaNs
        # are created. We do not want that.
        if not value.index.equals(data.index):
            if len(value) == len(data):
                value.index = data.index
            else:
                value.reset_index(drop=True, inplace=True)

    # You cannot assign a scalar value to a dataframe
    # without an index. You need an interable value.
    if data.index.empty:
        try:
            len(value)
        except TypeError:
            scalar = True
        else:
            scalar = isinstance(value, str)

        if scalar:
            value = [value]

    data[col] = value
    return data
def _create_column ( data , col , value ) : with suppress ( AttributeError ) : if not value . index . equals ( data . index ) : if len ( value ) == len ( data ) : value . index = data . index else : value . reset_index ( drop = True , inplace = True ) if data . index . empty : try : len ( value ) except TypeError : scalar = True else : scalar = isinstance ( value , str ) if scalar : value = [ value ] data [ col ] = value return data
Create column in dataframe

    Helper method meant to deal with problematic
    column values. e.g When the series index does
    not match that of the data.

    Parameters
    ----------
    data : pandas.DataFrame
        dataframe in which to insert value
    col : column label
        Column name
    value : object
        Value to assign to column

    Returns
    -------
    data : pandas.DataFrame
        Modified original dataframe

    >>> df = pd.DataFrame({'x': [1, 2, 3]})
    >>> y = pd.Series([11, 12, 13], index=[21, 22, 23])

    Data index and value index do not match

    >>> _create_column(df, 'y', y)
       x   y
    0  1  11
    1  2  12
    2  3  13

    Non-empty dataframe, scalar value

    >>> _create_column(df, 'z', 3)
       x   y  z
    0  1  11  3
    1  2  12  3
    2  3  13  3

    Empty dataframe, scalar value

    >>> df = pd.DataFrame()
    >>> _create_column(df, 'w', 3)
       w
    0  3
    >>> _create_column(df, 'z', 'abc')
       w    z
    0  3  abc