Pandas Python列拼接异常求助:如何去除拼接结果中的小数点
Hey there! The issue you're facing happens because your Income_Status_Number, Income_Stability_Number, and Product_Takeup_Number columns are probably stored as float types. When you convert floats like 1.0 directly to strings, they retain the .0 suffix, leading to concatenated results like 1.012 instead of 112. Here are three reliable solutions to get the clean, decimal-free strings you want:
Solution 1: Convert to Integer First (Best for Whole-Number Floats)
If all your values are whole numbers stored as floats (like 1.0, 2.0), converting them to integers first will strip the decimal part automatically before you turn them into strings:
df['Permutation'] = ( df['Income_Status_Number'].astype(int).astype(str) + df['Income_Stability_Number'].astype(int).astype(str) + df['Product_Takeup_Number'].astype(int).astype(str) )
This works because 1.0 becomes 1 when cast to an integer, then "1" as a string—perfect for clean concatenation.
Solution 2: Remove .0 with String Replacement
If you want to avoid converting to integers (e.g., in case of edge cases with non-whole numbers, though your example suggests otherwise), you can use string replacement to strip the trailing .0 from each value:
df['Permutation'] = ( df['Income_Status_Number'].astype(str).str.replace(r'\.0$', '') + df['Income_Stability_Number'].astype(str).str.replace(r'\.0$', '') + df['Product_Takeup_Number'].astype(str).str.replace(r'\.0$', '') )
The regex r'\.0$' matches exactly .0 at the end of each string and replaces it with nothing, leaving you with just the integer part.
Solution 3: Use Lambda with Formatted Strings
Another flexible approach is to use apply with a lambda function that formats each value as an integer string directly:
df['Permutation'] = df.apply( lambda row: f"{int(row['Income_Status_Number'])}{int(row['Income_Stability_Number'])}{int(row['Product_Takeup_Number'])}", axis=1 )
This method is great if you want to keep the logic concise in one line, and it ensures each value is treated as an integer before concatenation.
Quick Note
If your columns contain missing values (NaN), make sure to handle them first (e.g., df['Income_Status_Number'] = df['Income_Status_Number'].fillna(0)) to avoid errors when converting to integers or strings.
内容的提问来源于stack exchange,提问作者Saara Ligamena

