将带有逗号的 String31 小数转换为浮点数

问题描述 投票:0回答:1

我有一个包含 190 列、格式为 String31 的数据框?使用逗号作为小数点分隔符。我需要用句点替换这些逗号,然后将它们转换为数字格式。

我该怎么做?

我一直在尝试这样的事情:

function custom_parse(x::String, default::Float64)
    try
        return parse(Float64, x)
    catch
        return default
    end
end


for col in names(csv_coop_completo_1993a2022)[4:end]

    csv_coop_completo_1993a2022[!, col] = replace.(string.(csv_coop_completo_1993a2022[!, col]), "," => ".")
    
    csv_coop_completo_1993a2022[!, col] = map(x -> custom_parse(x, 0.0), csv_coop_completo_1993a2022[!, col])
end

但是我的结果以字符串格式显示,如

2.0361331369e8

csv

cnpj,ano,razao_social,10000007,11000006,11200002,39999993,60000002,61000001,61100004
171973,199312,COOPSCET,"5651,44","5651,44","5651,44","5651,44","5651,44","5651,44","8994,50"
379891,199312,CECMF CIMENTO TOCANTINS LTDA,"7429594,11","2759951,05","2759951,05","7429594,11","7161085,08","7161085,08","12550184,27"
617290,199312,"CECME SENAI-DF, LTDA","1500909,37","86432,56","86432,31","2632612,27","1573059,16","1573059,16","157922,83"
694877,199312,CECM SERV EXEC FED SECSA�DE DF,"22638658,49","306499,15","304001,15","25747440,55","24006473,31","24006473,31","20193139,66"
968602,199312,CCLA VALE S�O PATRICIO LTDA,"205124653,22","160604715,50","160469198,11","247012935,13","127063766,45","127063766,45","85617744,60"
julia decimal
1个回答
0
投票

我不知道我是否完全遵循你的思路。您似乎希望将数字转换为 Float64。您似乎也可能对科学计数法感到困惑(不过我对此不太确定)。

以下是如何加载数据,然后将相关列转换为 Float64:

# load libraries
using WeakRefStrings, CSV, DataFrames

# read in data
data = CSV.read("/Users/myusername/Downloads/example.csv", DataFrame)

# from the fourth column to the end, replace commas with periods, and parse as Float64
[data[!,i] = parse.(Float64, replace.(data[!,i], "," => ".")) for i in 4:size(data,2)]

输出:

5×10 DataFrame
 Row │ cnpj    ano     razao_social                    10000007      11000006       11200002       39999993      60000002       ⋯
     │ Int64   Int64   String                          Float64       Float64        Float64        Float64       Float64        ⋯
─────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 171973  199312  COOPSCET                        5651.44        5651.44        5651.44       5651.44       5651.44        ⋯
   2 │ 379891  199312  CECMF CIMENTO TOCANTINS LTDA       7.42959e6      2.75995e6      2.75995e6     7.42959e6     7.16109e6
   3 │ 617290  199312  CECME SENAI-DF, LTDA               1.50091e6  86432.6        86432.3           2.63261e6     1.57306e6
   4 │ 694877  199312  CECM SERV EXEC FED SECSA�DE DF     2.26387e7      3.06499e5      3.04001e5     2.57474e7     2.40065e7
   5 │ 968602  199312  CCLA VALE S�O PATRICIO LTDA        2.05125e8      1.60605e8      1.60469e8     2.47013e8     1.27064e8   ⋯
                                                                                                                2 columns omitted
© www.soinside.com 2019 - 2024. All rights reserved.