Pandas中round函数保留两位小数时部分列无变化的问题咨询
Pandas中round函数保留两位小数时部分列无变化的问题咨询
大家好,我在处理Pandas数据时遇到了一个小问题,想请各位帮忙分析下~
下面这段代码摘自我的程序,原本是想把DataFrame里的float64类型列统一格式化为保留两位小数,但奇怪的是,处理Height、Width、Depth这几列(对应代码里第24-26行的操作)时,数值完全没有发生变化。
我整理了一个最小复现示例,各位可以参考:
import os import pandas as pan nlf = pan.read_csv("New Lines.csv") nlf = nlf.iloc[:, 2:] total_rows = len(nlf) rows_to_drop = list(range(total_rows - 12, total_rows)) nlf = nlf.drop(nlf.index[rows_to_drop]) nlf.iloc[:, 14] = nlf.iloc[:, 14].round(2) # costs column nlf.iloc[:, 20] = nlf.iloc[:, 20].round(2) # TWC Stores column nlf.iloc[:, 21] = nlf.iloc[:, 21].round(2).copy() # RRP column nlf.iloc[:, 22] = (nlf.iloc[:, 22] * 100).round(2) # GP% column nlf.iloc[:, 23] = nlf.iloc[:, 23].round(2) # Weight column nlf.iloc[:, 24] = nlf.iloc[:, 24].round(2) # Height column nlf.iloc[:, 25] = nlf.iloc[:, 25].round(2) # Width column nlf.iloc[:, 26] = nlf.iloc[:, 26].round(2) #Depth column nlf.to_csv("nlf_decimal places.csv", index=False)
有没有大佬能帮我看看这是怎么回事呀?是不是我对round函数的使用存在误区,或者这些列本身的数据有什么特殊情况导致无法生效?
备注:内容来源于stack exchange,提问作者Nolan Padalhin
相关产品推荐
相关产品推荐

