This commit is contained in:
2024-09-27 15:23:56 +07:00
parent f8a72427fe
commit 564ed3cb70
3 changed files with 651 additions and 283 deletions

View File

@@ -6,7 +6,7 @@ export noNegative!, randomWithProb, randomChoiceWithProb, findIndex, limitvalue,
matMul_3Dto4D_batchwise, isNotEqual, linearToCartesian, vectorMax,
multiply_last, multiplyRandomElements, replaceElements, replaceElements!, isBetween,
isLess, allTrue, getStringBetweenCharacters, JSON3read_stringKey, mkDictPath!,
getDictPath, dataframeToCSV, dfToJSONRows
getDictPath
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames, CSV
using ..util, ..communication
@@ -1149,63 +1149,6 @@ function getDictPath(dict::Dict, keys::Vector)
end
""" Convert a dataframe into CSV.
# Arguments
- `df::DataFrame`
A connection object to Postgres database
# Return
- `result::String`
# Example
```jldoctest
julia> using DataFrames, GeneralUtils
julia> df = DataFrame(A=1:3, B=5:7, fixed=1)
julia> result = GeneralUtils.dataframeToCSV(df)
```
# Signature
"""
function dataframeToCSV(df::DataFrame)
# Create an IOBuffer to capture the output
io = IOBuffer()
CSV.write(io, df)
dfStr = String(take!(io))
return dfStr
end
""" Convert a DataFrame into a list of JSON rows.
# Arguments
- `df::DataFrame`
The input DataFrame to be converted.
# Return
- `rows::Vector{Dict{String, Any}}`
A vector of dictionaries, where each dictionary represents a row in JSON format.
# Example
```jldoctest
julia> using DataFrame, JSON3
julia> df = DataFrame(A = [1, 2, 3], B = ["apple", "banana", "cherry"])
julia> json_rows = dfToJSONRows(df)
```
# Signature
"""
function dfToJSONRows(df::DataFrame)
rows = []
for row in eachrow(df)
json_row = Dict{String, Any}()
for col in names(df)
json_row[col] = row[col]
end
push!(rows, json_row)
end
return rows
end