Dij*_*e85 5 string replace julia dataframes.jl
在 Python pandas 中,您可以传递一个字典, df.replace以便将每个匹配的键替换为其相应的值。我经常使用这个功能来替换西班牙语中的单词缩写,这些缩写会弄乱句子标记器。
朱莉娅身上有类似的东西吗?或者甚至更好,以便我(和未来的用户)可以从经验中学习,关于如何用 Julia 漂亮且高性能的语法实现这样的功能有什么想法吗?
\n谢谢你!
\n编辑:根据要求添加示例
\n输入:
\njulia> DataFrames.DataFrame(Dict("A" => ["This is an ex.", "This is a samp.", "This is a samp. of an ex."]))\n3\xc3\x971 DataFrame\n Row \xe2\x94\x82 A \n \xe2\x94\x82 String \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\n 1 \xe2\x94\x82 This is an ex.\n 2 \xe2\x94\x82 This is a samp.\n 3 \xe2\x94\x82 This is a samp. of an ex.\nRun Code Online (Sandbox Code Playgroud)\n期望的输出:
\n3\xc3\x971 DataFrame\n Row \xe2\x94\x82 A \n \xe2\x94\x82 String \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\n 1 \xe2\x94\x82 This is an example\n 2 \xe2\x94\x82 This is a sample\n 3 \xe2\x94\x82 This is a sample of an example\nRun Code Online (Sandbox Code Playgroud)\n
在 Julia 中,此功能也是replace. 它需要一个集合并替换其中的元素。最简单的形式是:
julia> x = ["a", "ab", "ac", "b", "bc", "bd"]\n6-element Vector{String}:\n "a"\n "ab"\n "ac"\n "b"\n "bc"\n "bd"\n\njulia> replace(x, "a" => "aa", "b" => "bb")\n6-element Vector{String}:\n "aa"\n "ab"\n "ac"\n "bb"\n "bc"\n "bd"\nRun Code Online (Sandbox Code Playgroud)\n如果您有更复杂的替换模式,您可以传递一个执行替换的函数:
\njulia> replace(x) do s\n length(s) == 1 ? s^2 : s\n end\n6-element Vector{String}:\n "aa"\n "ab"\n "ac"\n "bb"\n "bc"\n "bd"\nRun Code Online (Sandbox Code Playgroud)\n还有一个replace!就地做同样的事情。
这是你想要的吗?
\n替换字符串向量中的子字符串:
\njulia> df = DataFrame("A" => ["This is an ex.", "This is a samp.", "This is a samp. of an ex."])\n3\xc3\x971 DataFrame\n Row \xe2\x94\x82 A\n \xe2\x94\x82 String\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\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n 1 \xe2\x94\x82 This is an ex.\n 2 \xe2\x94\x82 This is a samp.\n 3 \xe2\x94\x82 This is a samp. of an ex.\n\njulia> df.A .= replace.(df.A, "ex." => "example", "samp." => "sample")\n3-element Vector{String}:\n "This is an example"\n "This is a sample"\n "This is a sample of an example"\nRun Code Online (Sandbox Code Playgroud)\n注意两件事:
\nDict给DataFrame构造函数。只需传递对就足够了。.=not =,它对已经存在的向量中的更新值进行就地替换(我将其显示出来是为了与 @Sundar R 在评论中提出的内容进行比较,这是分配新向量的替代方案;差异可能对于你的情况来说并不重要,但我只是想向你展示两种语法)。| 归档时间: |
|
| 查看次数: |
561 次 |
| 最近记录: |