带有标题的数组中的DataFrame [英] DataFrame from Array with Header
本文介绍了带有标题的数组中的DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有标题行的数组:
I have an array with a header row say:
arr = ["age" "height" "weight";
20 175 73;
30 160 66]
并且我想将其转换为DataFrame,推荐的方法是什么? 我目前的方法是下面的答案,还有更好的建议吗?
and I want to convert it to a DataFrame, what is the recommended method? My current method is an answer below, any better recommendations?
推荐答案
一线:
# convert a Matrix{Any} with a header row of col name strings to a DataFrame
# e.g. mat2df(["a" "b" "c"; 1 2 3; 4 5 6])
mat2df(mat) =
DataFrame([[mat[2:end,i]...] for i in 1:size(mat,2)], Symbol.(mat[1,:]))
# convert a Matrix and a list of col name strings to a DataFrame
# e.g. matncolnames2df([1 2 3;4 5 6], ["a","b","c"])
matncolnames2df(mat, colnames) =
DataFrame([[mat[:,i]...] for i in 1:size(mat,2)], Symbol.(colnames))
Re:Iain Dunning在下面的评论:曾经有一个宏,但是不赞成使用它调用的DataFrame构造函数,并且上面的方法工作良好,所以现在消失了……(不用担心,那不是很好,我认为Iain Dunning很好:)
Re: Iain Dunning's comment below: There was a macro here once but the DataFrame constructor it called got deprecated and the above works well, so now it's gone... (Don't worry, it wasn't that good, I think Iain Dunning was just being nice :)
这篇关于带有标题的数组中的DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文