Pandas Mixed Type to Integer

前端 未结 2 1492
清歌不尽
清歌不尽 2021-01-21 15:06

Given the following data frame:

import pandas as pd
df = pd.DataFrame(
        {\'A\':[\'A\',\'B\',\'C\',\'D\'],
         \'C\':[\'1\',\'12\',\'*\',\'8\']
               


        
相关标签:
2条回答
  • 2021-01-21 15:53

    int() is the Python standard built-in function to convert a string into an integer value. Convert the column to int using int().

    For parsing integers instead of floats, you can use the isdigit() function for string objects.

    If you run isdigit() after int(), you can filter the data to rows where the value for column C is an integer.

    0 讨论(0)
  • 2021-01-21 15:57

    You could use pd.to_numeric to convert the C column to numeric values. Passing errors='coerce' tells pd.to_numeric to set non-numeric values to NaN.

    import pandas as pd
    
    df = pd.DataFrame(
            {'A':['A','B','C','D'],
             'C':['1','12','*','8'] })
    
    df['C'] = pd.to_numeric(df['C'], errors='coerce')
    print(df)
    

    prints

       A     C
    0  A   1.0
    1  B  12.0
    2  C   NaN
    3  D   8.0
    

    Since NaN values are only allowed in columns with floating-point dtype (or object dtype), the column can not be set to an integer dtype.

    0 讨论(0)
提交回复
热议问题