[Python] Cannot get dataframe.['col1].median() to ignore nulls

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

  1. Stack

    Stack Membro Participativo

    I have pandas dataframe df1 with a 'rating' column. I Need the median of that column. The column contains the string 'Not Given' for missing values. I have replaced the 'Not given' values Numpy's np.nan and None using the Line:

    df1.loc[df1['rating'] == 'Not given', 'rating'] = np.nan`
    -OR-
    df1.loc[df1['rating'] == 'Not given', 'rating'] = None`
    and verified the results. Then tried to get the median of df1['rating'] using this line:
    mrat_y =df1['rating'].median( skipna = True)



    The error message I get using either null substitute is:

    {File "C:\Users\fnaso\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\nanops.py", line 787, in nanmedian
    raise TypeError(f"Cannot convert {values} to numeric")
    TypeError: Cannot convert [nan nan '5' ... nan '5' nan] to numeric
    }



    I am using Pycharm community edition with the latest Python version.

    So either the replacements are not valid Nulls or median( skipna = True) is working as I expect or (most likely) I am missing something with my newbie head up my ass.

    This works with theW3Schools_Tryit_Editor.

    I was expecting to find the median of a panda dataframe column that has float8 and Null values.

    Thank you all in advance. I hope some day that can contribute to this fine forum.

    Continue reading...

Compartilhe esta Página