gpt4 book ai didi

python pandas read_csv 千位分隔符不起作用

转载 作者:行者123 更新时间:2023-12-01 03:18:00 26 4
gpt4 key购买 nike

我使用 pandas read_csv 来读取一个简单的 csv 文件。但是,它有 ValueError: Could not conversion string to float: 我不明白为什么。

代码很简单

rawdata = pd.read_csv( r'Journal_input.csv' ,
dtype = { 'Base Amount' : 'float64' } ,
thousands = ',' ,
decimal = '.',
encoding = 'ISO-8859-1')

但我收到此错误

pandas\parser.pyx in pandas.parser.TextReader.read (pandas\parser.c:10415)()

pandas\parser.pyx in pandas.parser.TextReader._read_low_memory (pandas\parser.c:10691)()

pandas\parser.pyx in pandas.parser.TextReader._read_rows (pandas\parser.c:11728)()

pandas\parser.pyx in pandas.parser.TextReader._convert_column_data (pandas\parser.c:13162)()

pandas\parser.pyx in pandas.parser.TextReader._convert_tokens (pandas\parser.c:14487)()

ValueError: could not convert string to float: '79,026,695.50'

将字符串“79,026,695.50”转换为 float 时怎么会出错?我已经指定了两个选项

thousands = ',' ,
decimal = '.',

是我的代码有问题还是 pandas 中的错误?

最佳答案

似乎引用有问题,因为如果分隔符是,并且thousands也是,,一些引用必须在 csv 中:

import pandas as pd
from pandas.compat import StringIO
import csv

temp=u"""'a','Base Amount'
'11','79,026,695.50'"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),
dtype = { 'Base Amount' : 'float64' },
thousands = ',' ,
quotechar = "'",
quoting = csv.QUOTE_ALL,
decimal = '.',
encoding = 'ISO-8859-1')

print (df)
a Base Amount
0 11 79026695.5

temp=u'''"a","Base Amount"
"11","79,026,695.50"'''
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),
dtype = { 'Base Amount' : 'float64' },
thousands = ',' ,
quotechar = '"',
quoting = csv.QUOTE_ALL,
decimal = '.',
encoding = 'ISO-8859-1')

print (df)
a Base Amount
0 11 79026695.5

关于python pandas read_csv 千位分隔符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42293060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com