Python中如何不四舍五入保留两位小数?(pd.Series场景)
round()? Got it, you're looking to truncate a pandas Series to exactly two decimal places without using round() (so no rounding up—like turning 2.059999 into 2.05 instead of 2.06). Let's go through a few reliable methods that work for this:
Method 1: Mathematical Truncation (Fastest for Numeric Data)
This is probably the most efficient way, using basic arithmetic to shift the decimal point, truncate the extra digits, then shift back:
import pandas as pd # Your input Series s = pd.Series([1.42345, 12.33444, 111.66777, 2.059999]) # Truncate to 2 decimals truncated_series = (s * 100).astype(int) / 100 print(truncated_series) # Output: # 0 1.42 # 1 12.33 # 2 111.66 # 3 2.05 # dtype: float64
How it works: Multiplying by 100 moves the first two decimal places into the integer part. Converting to int() drops any remaining decimal digits (no rounding), then dividing by 100 puts the decimal back in place.
Method 2: Using NumPy's trunc Function
If you're working with large datasets, NumPy's vectorized functions can be slightly faster. The logic is the same as above, but using np.trunc() explicitly:
import pandas as pd import numpy as np s = pd.Series([1.42345, 12.33444, 111.66777, 2.059999]) truncated_series = np.trunc(s * 100) / 100 print(truncated_series) # Same output as above
np.trunc() directly truncates the decimal part of a number, so this avoids converting to int() and works consistently for positive and negative numbers (if you ever need that).
Method 3: Improved String Manipulation
You mentioned struggling with string slicing because integer parts vary in length—here's a workaround that splits the string at the decimal point to handle any integer length:
import pandas as pd s = pd.Series([1.42345, 12.33444, 111.66777, 2.059999]) def truncate_to_two_decimals(x): parts = str(x).split('.') if len(parts) == 1: # Handle integers (e.g., 5 becomes 5.00) return f"{parts[0]}.00" else: # Take integer part + first two decimal digits return f"{parts[0]}.{parts[1][:2]}" truncated_series = s.apply(truncate_to_two_decimals).astype(float) print(truncated_series)
This method is more flexible if you need to handle edge cases like whole numbers, but it's slower than the mathematical approaches since it involves string operations.
On "Displaying Floats Without Rounding"
If you just need to display the values with two decimal places (without modifying the underlying data), you can format the output directly—but note that standard string formatting like "{:.2f}" uses rounding. To display truncated values, you'll want to process the data first with one of the methods above, then format for display:
# After truncating with Method 1 print(truncated_series.to_string(float_format="{:.2f}")) # Output: # 0 1.42 # 1 12.33 # 2 111.66 # 3 2.05
As you noted, many existing solutions (like the one in "Truncate to three decimals in Python") involve rounding, so these methods specifically avoid that by truncating instead.
内容的提问来源于stack exchange,提问作者user8560167




