{"id":19615016,"url":"https://github.com/ghurtchu/parcsv","last_synced_at":"2025-04-28T02:31:06.631Z","repository":{"id":64171300,"uuid":"573887896","full_name":"Ghurtchu/parcsv","owner":"Ghurtchu","description":":open_file_folder::newspaper: Manipulate CSV dataframes.","archived":false,"fork":false,"pushed_at":"2022-12-21T09:25:01.000Z","size":1322,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2023-12-08T00:08:53.011Z","etag":null,"topics":["csv","data-manipulation","dataframe","etl","scala"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ghurtchu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-12-03T18:44:11.000Z","updated_at":"2023-12-08T00:08:53.012Z","dependencies_parsed_at":"2023-01-15T02:01:01.272Z","dependency_job_id":null,"html_url":"https://github.com/Ghurtchu/parcsv","commit_stats":null,"previous_names":["scalevolvable/parcsv","ghurtchu/parcsv"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2Fparcsv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2Fparcsv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2Fparcsv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2Fparcsv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ghurtchu","download_url":"https://codeload.github.com/Ghurtchu/parcsv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224092034,"owners_count":17254152,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","data-manipulation","dataframe","etl","scala"],"created_at":"2024-11-11T10:54:54.973Z","updated_at":"2024-11-11T10:54:55.869Z","avatar_url":"https://github.com/Ghurtchu.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"`parcsv` is a parser which treats CSV files as tabular dataframes enabling you to manipulate them fairly easily.\n\nparcsv is based on `Either[Throwable, CSV]` to enable you to do functional style CSV processing.\n\nTypical flow:\n - Read CSV from different sources (File, Raw String, Map etc..)\n - Process it\n - Save it\n\nScala code:\n\n```scala\nimport com.ghurtchu.csv._\n\n// csv as a raw string\nval source =\n  \"\"\"food,calories,protein,carbs,isHealthy\n    |apple,52,0.3,14,true\n    |egg,155,13,26,true\n    |potato,77,4.3,26,true\n    |sugar,387,0,100,false\n    |\"\"\".stripMargin\n\n// only keep those columns which contain \"o\" in their header value\nval containsLetter: Column =\u003e Boolean = col =\u003e col.header.value.contains(\"o\")\n\n// only keeps those rows which have a value less than 10 under \"protein\" header\nval lowProteinFoodFilter: Cell =\u003e Boolean = cell =\u003e cell.belongsTo(\"protein\") \u0026\u0026 cell.numericValue \u003c= 10\n\n// csv processing\nval newCSV = for {\n  csv      \u003c- CSV.fromString(source) // read\n  cleaned  \u003c- csv.filterColumns(containsLetter) // drop \"carbs\" and \"isHealthy\"\n  filtered \u003c- cleaned.filterRows(lowProteinFoodFilter) // take rows with \"protein\" value less than 10\n  sorted   \u003c- filtered.sortByColumn(\"calories\", SortOrdering.Desc) // sort by \"calories\" in descending numeric order\n  _        \u003c- sorted.display // print it\n  _        \u003c- sorted.save(\"data/processed_food.csv\") //save it\n} yield sorted\n```\n\nPrint result:\n\n![My Image](screenshot_food.png)\n\nLet's see an advanced example using `Pipes`.\n\n`Pipe` is useful when there is a need to apply more than 1 transformation to CSV. \nThey hold filter/map/reduce functions which will be applied sequentially to CSV files.\n\nIn this example we use:\n\n- `FilterColumnPipe` - filters columns sequentially\n- `FilterRowPipe` - filters rows sequentially\n- `TransformColumnPipe` - transforms rows sequentially\n\nScala code:\n\n```scala\nimport com.ghurtchu.csv._\n\nval filterColumnPipe = FilterColumnPipe(\n  col =\u003e Seq(\"name\", \"popularity\", \"creator\").contains(col.header.value), // choose columns by names\n  col =\u003e col.cells.forall(_.value.length \u003c= 20) // keep columns with all values shorter than 20 characters\n)\n\nval filterRowPipe = FilterRowPipe(\n  row =\u003e row.index % 2 == 1, // then take only odd-indexed rows\n  row =\u003e row.isFull // then keep those which have no N/A-s\n)\n\nval transformColumnPipe = TransformColumnPipe(\n  col =\u003e Column(col.header, col.cells.map(cell =\u003e cell.copy(value = cell.value.toUpperCase))) // make all values uppercase\n)\n\n// create bigger pipe by joining from left to right\nval fullPipe = filterColumnPipe ~\u003e filterRowPipe ~\u003e transformColumnPipe\n\n// processing\nval transformedCSV = for {\n  csv         \u003c- CSV.fromFile(\"data/programming_languages.csv\") // read\n  transformed \u003c- csv.transformVia(fullPipe) // apply the whole pipe\n  _           \u003c- transformed.display // print it \n  _           \u003c- transformed.save(\"data/updated.csv\") // save\n} yield transformed\n\n```\n\nPrint result:\n\n![My Image](screenshot_languages.png)\n\n\nTODO:\n - slicing\n - add `CSVError` hierarchy to replace `Throwable` in `Either[Throwable, CSV]`\n - and much more...\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghurtchu%2Fparcsv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghurtchu%2Fparcsv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghurtchu%2Fparcsv/lists"}