PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] How do you read a value at a specific index of a TabularData Dataframe?

 November 02, 2022     csv, indexing, swift, tabular     No comments   

Issue

I am a beginner in Swift, and I just learned I could import csv with tabular data. However, even though I now have a slice (I am not sure what the specific type is), I can't figure out how to access an index in there. I want to read row 0 and column 4 (location).

Picture of the Dataframe slice:

Picture of the Dataframe slice

var convert = try DataFrame(contentsOfCSVFile: URL(string: "https://raw.githubusercontent.com/shadowdragon2805/CrimeData/main/docs/csv/zip_code_database.csv")!, columns: columns, types: ["zip": .string, "primary_city": .string, "state": .string, "county": .string, "latitude": .double, "longitude": .double])
let new = convert.filter(on: "zip", String.self){$0 == "00501"}

new is the name of my slice that I just filtered. I want to access an index of this table but I can't. I have tried many ways but all have given me an error:


let index = new.subscript(r: 0, c: 4)

but that results in the error:

Cannot call value of non-function type 'AnyColumnSlice'

let index = new.index(0, 4)

also results in:

Cannot call value of non-function type 'AnyColumnSlice'

let index = new(row: 0, column: 4)

but that results in:

Cannot call value of non-function type 'DataFrame.Slice'

let index = new[0][4]

but that results in:

Cannot convert value of type 'Int' to expected argument type 'String'

How can I access index [0][4] so I can find the location on this table?


Solution

There are many ways of accessing a specific value in a data frame slice. The closest to your attempts is probably:

let latitude = new.rows[0][4, Double.self]

Note that new[row: 0][4, Double.self] would have worked if new were a DataFrame, but unfortunately new is a DataFrame.Slice here which doesn't have the subscript(row:) subscript for some reason.

You can also access the column by name, which I recommend:

let latitude = new.row[0]["latitude", Double.self]

You can also access the column first:

let latitude = new["latitude", Double.self][0]
// or
let latitude = new[column: 4, Double.self][0]


Answered By - Sweeper
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing