update
This commit is contained in:
183
src/util.jl
183
src/util.jl
@@ -2,7 +2,8 @@ module util
|
||||
|
||||
export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys,
|
||||
findMatchingDictKey, textToDict, randstring, randstrings, timeout,
|
||||
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict
|
||||
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString,
|
||||
dfToString, dataframe_to_json_list, dict_to_string
|
||||
|
||||
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
|
||||
|
||||
@@ -522,8 +523,188 @@ end
|
||||
|
||||
|
||||
|
||||
""" Get a value from a DataFrame row by a given key
|
||||
|
||||
# Arguments
|
||||
- `row::DataFrameRow`
|
||||
The DataFrame row to retrieve the value from.
|
||||
- `key::Symbol`
|
||||
The column name (as a symbol) whose value is to be retrieved.
|
||||
|
||||
# Return
|
||||
- `Any`
|
||||
The value of the specified column in the given row.
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataFrames
|
||||
|
||||
julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30])
|
||||
2×2 DataFrame
|
||||
Row │ name age
|
||||
│ String Int64
|
||||
┌─────┼─────────┼───────
|
||||
│ 1 │ Alice 25
|
||||
│ 2 │ Bob 30
|
||||
|
||||
julia> getDataFrameValue(df[1, :], :name)
|
||||
"Alice"
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
getDataFrameValue(row::DataFrameRow, key::Symbol) = row.:($key)
|
||||
|
||||
|
||||
""" Convert a DataFrame row to a key:value string
|
||||
|
||||
# Arguments
|
||||
- `row::DataFrameRow`
|
||||
The DataFrame row to convert.
|
||||
|
||||
# Return
|
||||
- `String`
|
||||
A string containing the formatted representation of the row, with each column prefixed by its name and separated by commas.
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataFrames
|
||||
|
||||
julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30])
|
||||
2×2 DataFrame
|
||||
Row │ name age
|
||||
│ String Int64
|
||||
┌─────┼─────────┼───────
|
||||
│ 1 │ Alice 25
|
||||
│ 2 │ Bob 30
|
||||
|
||||
julia> dfRowtoString(df[1, :])
|
||||
"name: Alice, age: 25"
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function dfRowtoString(row::DataFrameRow)::String
|
||||
str = ""
|
||||
for key in keys(row)
|
||||
value = getDataFrameValue(row, key)
|
||||
str *= "$key: $value, "
|
||||
end
|
||||
result = str[1:end-2] # remove ", " at the end of row
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
""" Convert a DataFrame to a string representation
|
||||
|
||||
# Arguments
|
||||
- `df::DataFrame`
|
||||
The DataFrame to convert, where each row will be converted to a string.
|
||||
|
||||
# Return
|
||||
- `String`
|
||||
A string containing the formatted representation of the DataFrame, with each row prefixed by its index and separated by newlines.
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataFrames
|
||||
|
||||
julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30])
|
||||
2×2 DataFrame
|
||||
Row │ name age
|
||||
│ String Int64
|
||||
┌─────┼─────────┼───────
|
||||
│ 1 │ Alice 25
|
||||
│ 2 │ Bob 30
|
||||
|
||||
julia> dfToString(df)
|
||||
"1) name: Alice, age: 25\n2) name: Bob, age: 30"
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function dfToString(df::DataFrame)
|
||||
dfstr = ""
|
||||
for (i, row) in enumerate(eachrow(df))
|
||||
rowstr = dfRowtoString(row)
|
||||
dfstr *= "$i) $rowstr\n"
|
||||
end
|
||||
return dfstr
|
||||
end
|
||||
|
||||
|
||||
""" Convert a DataFrame to a list of JSON strings
|
||||
|
||||
# Arguments
|
||||
- `df::DataFrame`
|
||||
The DataFrame to convert, where each row will be converted to a JSON string.
|
||||
|
||||
# Return
|
||||
- `Vector{String}`
|
||||
A vector containing the JSON representation of each row in the DataFrame.
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataFrames
|
||||
|
||||
julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30])
|
||||
2×2 DataFrame
|
||||
Row │ name age
|
||||
│ String Int64
|
||||
┌─────┼─────────┼───────
|
||||
│ 1 │ Alice 25
|
||||
│ 2 │ Bob 30
|
||||
|
||||
julia> dataframe_to_json_list(df)
|
||||
2-element Vector{String}:
|
||||
"{\"name\":\"Alice\",\"age\":25}"
|
||||
"{\"name\":\"Bob\",\"age\":30}"
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function dataframe_to_json_list(df::DataFrame)::Vector{String}
|
||||
json_list = []
|
||||
for row in eachrow(df)
|
||||
json_row = Dict(zip(names(df), row))
|
||||
push!(json_list, JSON.json(json_row))
|
||||
end
|
||||
return json_list
|
||||
end
|
||||
|
||||
|
||||
""" Convert a dictionary to a string representation.
|
||||
|
||||
# Arguments
|
||||
- `od::OrderedDict`
|
||||
The OrderedDict to convert, where each key-value pair will be represented as "index) key: value".
|
||||
|
||||
# Return
|
||||
- `String`
|
||||
A string containing the representation of each key-value pair in the OrderedDict.
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataStructures
|
||||
|
||||
julia> od = OrderedDict("name" => "Alice", "age" => 25)
|
||||
OrderedDict{String,Any} with 2 entries:
|
||||
"name" => "Alice"
|
||||
"age" => 25
|
||||
|
||||
julia> dict_to_string(od)
|
||||
"1) name: Alice, 2) age: 25"
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function dict_to_string(od::T) where {T<:AbstractDict}
|
||||
items = []
|
||||
for (i, (key, value)) in enumerate(od)
|
||||
push!(items, "$i) $key: $value")
|
||||
end
|
||||
return join(items, ", ")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user