Cannot place this error in dataframes

hi,

I have a simple dataframe

csv_file = ["MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY"]

df = DataFrame(NAMES=sort(csv_file))

I then want to transform (add a column that is based on the first column in the df,


###Takes the letters in the string and counts them up
function count_char(n::String)
    foldr(+,[Int(i)-64 for i in n])
end

But if I do this, I get an error:

@transform df begin
    :NAMES_NUMBER = count_char(:NAMES)
end


ERROR: LoadError: MethodError: no method matching Int64(::String)
Closest candidates are:
  Int64(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:708
  Int64(::Ptr) at boot.jl:718
  Int64(::Float32) at float.jl:706

I thought maybe its the function itself so I took a base function that should work on this:

@transform df begin
    :NAMES_NUMBER = lowercase(:NAMES)
end

ERROR: LoadError: MethodError: no method matching lowercase(::Array{String,1})
Closest candidates are:
  lowercase(::T) where T<:AbstractChar at strings/unicode.jl:245
  lowercase(::AbstractString) at strings/unicode.jl:531

I am new to dataframes so I think that is the reason. Could someone explain the essence behind this error (i assume they are the same, if not please correct me).

thanks!

You want @rtransform, for row-wise transformations, instead of @transform, which takes in the whole column.

julia> @rtransform df begin
           :NAMES_NUMBER = count_char(:NAMES)
       end
14×2 DataFrame
 Row │ NAMES      NAMES_NUMBER 
     │ String     Int64        
─────┼─────────────────────────
   1 │ BARBARA              43
   2 │ BETTY                72
   3 │ DOROTHY             105
   4 │ ELIZABETH            88
   5 │ JENNIFER             81
   6 │ KAREN                49
   7 │ LINDA                40
   8 │ LISA                 41
   9 │ MARGARET             83
  10 │ MARIA                42
  11 │ MARY                 57
  12 │ NANCY                57
  13 │ PATRICIA             77
  14 │ SUSAN                74

and the second example

julia> @rtransform df begin
           :NAMES_NUMBER = lowercase(:NAMES)
       end
14×2 DataFrame
 Row │ NAMES      NAMES_NUMBER 
     │ String     String       
─────┼─────────────────────────
   1 │ BARBARA    barbara
   2 │ BETTY      betty
   3 │ DOROTHY    dorothy
   4 │ ELIZABETH  elizabeth
   5 │ JENNIFER   jennifer
   6 │ KAREN      karen
   7 │ LINDA      linda
   8 │ LISA       lisa
   9 │ MARGARET   margaret
  10 │ MARIA      maria
  11 │ MARY       mary
  12 │ NANCY      nancy
  13 │ PATRICIA   patricia
  14 │ SUSAN      susan

See the docs here.

this did not seem to work for me but, this did:

 @transform df @byrow begin
           :NAMES_NUMBER = count_char(:NAMES)
       end

Thank you for pointing me in the right direction!

@rtransform was added after @byrow. So if you upgrade your version of DataFramesMeta.jl they should both work.

Or just

df.NAMES_NUMBER = count_chars.(df.NAMES)

Yes, that seems to work to :slight_smile: