How do we remove zeros after the comma in certain columns?



  • My code is:

    df_CG1=pd.read_csv('payments_CG1.csv' ,sep=';')
    pd.set_option('display.max_rows', None)
    df_CG1=df_CG1.dropna(how= 'all')
    df_CG1
    

    датафрейм

    Please indicate as a column: user_idrevenueand payment_service_id I'd like you to put "objectives" to look beautiful.



  • Maybe you get the type of data. float64'Cause in some lines, you're dating. NaN values and whole types in Numpy do not understand NaN and therefore Pandas automatically converts such columns into data type float64

    In modern versions, Pandas introduced a new type of data "Int64" (with a large letter) which understands the values NaN

    Example:

    In [13]: df = pd.DataFrame([[1, 2, 3], [np.nan, 3, np.nan]], columns=list("abc"))
    

    In [14]: df
    Out[14]:
    a b c
    0 1.0 2 3.0
    1 NaN 3 NaN

    In [17]: cols = df.columns[df.dtypes.eq("float64")]

    In [18]: df[cols] = df[cols].astype(int)

    ValueError Traceback (most recent call last)
    ...
    ValueError: Cannot convert non-finite values (NA or inf) to integer
    ...

    Decision:

    In [19]: df[cols] = df[cols].astype("Int64")

    In [20]: df
    Out[20]:
    a b c
    0 1 2 3
    1 <NA> 3 <NA>

    In [21]: df.dtypes
    Out[21]:
    a Int64
    b int64
    c Int64
    dtype: object



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2