[Python] Using pandas `.assign()` to make a column that contains a string scalar

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I recently stumbled upon the .assign() dataframe method and love how it can cleanly express creating new columns. It's very intuitive to create columns that are functions of other columns and objects, however, assigning a string scalar returns NaN for the entire column. This makes sense looking at the documentation that the method takes keyword arguments with a callable or series as values, but even when using a lambda to basically wrap the string into a function it returns a column of NaN values.


    str_scalar = "Hello"

    df = df.assign(str_scalar_col = str_scalar)


    # column str_scalar_col is all NaN


    df = df.assign(str_scalar_col = lambda x: str_scalar)

    # column str_scalar_col is still all NaN


    Maybe this has to do with the type of the column created by default?

    Normally I would just assign the column inplace, but curious if .assign() can assign a string scalar column.


    df['str_scalar'] = "Hello"

    # column str_scalar is all "Hello"

    Continue reading...

Compartilhe esta Página