Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using DataFrames, DataStructures
- m = [
- "parName" "region" "forType" "value";
- "vol" "AL" "broadL_highF" 3.3055628012;
- "vol" "AL" "con_highF" 2.1360975151;
- "vol" "AQ" "broadL_highF" 5.81984502;
- "vol" "AQ" "con_highF" 8.1462998309;
- ]
- function toDf1(m)
- h = [Symbol(c) for c in m[1,:]]
- vals = m[2:end, :]
- df = convert(DataFrame,OrderedDict(zip(h,[vals[:,i] for i in 1:size(vals,2)])))
- for c in names(df)
- # Try to convert df from Any to In64, Float64 or String (in that order)
- try
- df[c] = convert(DataArrays.DataArray{Int64,1},df[c])
- catch
- try
- df[c] = convert(DataArrays.DataArray{Float64,1},df[c])
- catch
- try
- df[c] = convert(DataArrays.DataArray{String,1},df[c])
- catch
- end
- end
- end
- end
- return df
- end
- function toDf2(m)
- s = join([join([m[i,j] for j in indices(m, 2)], '\t') for i in indices(m, 1)], '\n')
- df = DataFrames.inlinetable(s; separator='\t', header=true)
- return df
- end
- function toDf3(m)
- df = DataFrame()
- for (ind,s) in enumerate(Symbol.(m[1,:])) # convert first row to symbols and iterate through them.
- # check all types the same else assign to Any
- T = typeof(m[2,ind])
- T = all(typeof.(m[2:end,ind]).==T) ? T : Any
- # convert to type of second element then add to data frame
- df[s] = T.(m[2:end,ind])
- end
- return df
- end
- # second time for compilation.. further times ~ results
- @time toDf1(m) # 0.000946 seconds (336 allocations: 19.811 KiB)
- @time toDf2(m) # 0.000194 seconds (306 allocations: 17.406 KiB)
- @time toDf3(m) # 0.001820 seconds (445 allocations: 35.297 KiB)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement