我有一个 DataFrame dfPaths 如下(我正在使用 Julia)
我想将列 arc 定义为该数据帧的索引,因此稍后我可以使用 dfPaths[arc, "to"] 访问特定弧的列“to”。
我知道在这个例子中,arc = row,但在将来,我可能会对 arc 有不同的值,而不是连续的数字。
我尝试过使用setindex!但没有成功。我希望得到与 Python 中的 df.set_index() 相同的结果。
预先感谢您的任何帮助
DataFrames.jl 使用不同的设置索引的方法。
\n请执行下列操作:
\njulia> dfPaths = DataFrame(arc=["a", "b", "a", "c", "d"], from=[1,1,2,3,2], to=[2,3,3,2,4])\n5\xc3\x973 DataFrame\n Row \xe2\x94\x82 arc from to\n \xe2\x94\x82 String Int64 Int64\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n 1 \xe2\x94\x82 a 1 2\n 2 \xe2\x94\x82 b 1 3\n 3 \xe2\x94\x82 a 2 3\n 4 \xe2\x94\x82 c 3 2\n 5 \xe2\x94\x82 d 2 4\n\njulia> gdf = groupby(dfPaths, :arc);\n\njulia> gdf[("a",)]\n2\xc3\x973 SubDataFrame\n Row \xe2\x94\x82 arc from to\n \xe2\x94\x82 String Int64 Int64\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n 1 \xe2\x94\x82 a 1 2\n 2 \xe2\x94\x82 a 2 3\n\njulia> gdf[("b",)]\n1\xc3\x973 SubDataFrame\n Row \xe2\x94\x82 arc from to\n \xe2\x94\x82 String Int64 Int64\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n 1 \xe2\x94\x82 b 1 3\nRun Code Online (Sandbox Code Playgroud)\n注意:
\nonly(gdf[("b",)])["from"]那么如果它确实是唯一的,您将获得该列的值,如果它不是唯一的,则会出现错误。| 归档时间: |
|
| 查看次数: |
346 次 |
| 最近记录: |