我输入read_csv的csv文件是一对列,有百分比变化,但它有一些隐藏字符。来自repr(data2)
:
我尝试了以下方法:
data2 = pd.read_csv('C:/Users/nnayyar/Documents/MonteCarlo2.csv', "\n", delimiter = ",", dtype = float)
得到以下错误:
^{pr2}$我试了几件事:
float(data2.replace('/n',''))
map(float, data2.strip().split('\r\n'))
但分别收到了各种各样的错误 TypeError:float()参数必须是字符串或数字 AttributeError:“DataFrame”对象没有属性“strip”
任何将CSV对象类型转换为float类型的帮助都会很有帮助!谢谢!!在
如果您的整个csv都有百分号,则以下操作将起作用:
因此,这将对每个调用向量化
str.replace
的列应用lambda来删除%
符号,然后我们可以使用astype
将类型转换为float因此,在您的情况下,以下方法应该有效:
^{pr2}$相关问题 更多 >
编程相关推荐