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_id
♪revenue
andpayment_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 understandNaN
and therefore Pandas automatically converts such columns into data typefloat64
♪In modern versions, Pandas introduced a new type of data
"Int64"
(with a large letter) which understands the valuesNaN
♪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 NaNIn [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