1# convert all columns of DataFrame
2df = df.apply(pd.to_numeric) # convert all columns of DataFrame
3
4# convert just columns "a" and "b"
5df[["a", "b"]] = df[["a", "b"]].apply(pd.to_numeric)
1# (1) Round to specific decimal places – Single DataFrame column
2df['DataFrame column'].round(decimals=number of decimal places needed)
3
4# (2) Round up – Single DataFrame column
5df['DataFrame column'].apply(np.ceil)
6
7# (3) Round down – Single DataFrame column
8df['DataFrame column'].apply(np.floor)
9
10# (4) Round to specific decimals places – Entire DataFrame
11df.round(decimals=number of decimal places needed)
1# convert Series
2my_series = pd.to_numeric(my_series)
3
4# convert column "a" of a DataFrame
5df["a"] = pd.to_numeric(df["a"])
6
1>>> s = pd.Series(["8", 6, "7.5", 3, "0.9"]) # mixed string and numeric values
2>>> s
30 8
41 6
52 7.5
63 3
74 0.9
8dtype: object
9
10>>> pd.to_numeric(s) # convert everything to float values
110 8.0
121 6.0
132 7.5
143 3.0
154 0.9
16dtype: float64
17