注意:这个问题的确是一个重复的分离pandas数据帧字符串条目到单独的行,但这里提供的答案更通用和信息丰富,所以在所有方面到期,我选择不删除线程
我有一个'数据集',格式如下:
id | value | ...
--------|-------|------
a | 156 | ...
b,c | 457 | ...
e,g,f,h | 346 | ...
... | ... | ...
Run Code Online (Sandbox Code Playgroud)
我想通过复制每个ID的所有值来规范化它:
id | value | ...
--------|-------|------
a | 156 | ...
b | 457 | ...
c | 457 | ...
e | 346 | ...
g | 346 | ...
f | 346 | ...
h | 346 | ...
... | ... | ...
Run Code Online (Sandbox Code Playgroud)
我正在做的是应用split-apply-combine pandas使用原则,为每个组 …
我无法找到实现这一目标的优雅方式,请帮忙.
我有一个DTdata.table:
name,value
"lorem pear ipsum",4
"apple ipsum lorem",2
"lorem ipsum plum",6
Run Code Online (Sandbox Code Playgroud)
基于列表,Fruits <- c("pear", "apple", "plum")我想创建一个因子类型列.
name,value,factor
"lorem pear ipsum",4,"pear"
"apple ipsum lorem",2,"apple"
"lorem ipsum plum",6,"plum"
Run Code Online (Sandbox Code Playgroud)
我想这是基本的,但我有点卡住了,这是我得到了多远:
DT[grep("apple", name, ignore.case=TRUE), factor := as.factor("apple")]
提前致谢.
我只是无法围绕这个工作的 Lua snipplet。
这个:
t = {'a', 'b', 'c'}
for k, v in next, t, nil do
print(k, v)
end
Run Code Online (Sandbox Code Playgroud)
返回这个:
1 a
2 b
3 c
Run Code Online (Sandbox Code Playgroud)
谁能解释一下
next获取t作为其参数?t是有效的to参数fornil需要并被接受为有效步骤?我必须编写一个可以遍历嵌套表的迭代器.我写了一个使用coroutine.
它创建了一个(路径,值)对的数组,例如{{key1, key2, key3}, value}意味着要让value你去做nested_table[key1][key2][key3].
当我写find(),findall(),in()轻松,生活是光明的.
function table.extend(tbl, new_value)
local tbl = {table.unpack(tbl)}
table.insert(tbl, new_value)
return tbl
end
function iterate(tbl, parent)
local parent = parent or {}
if (type(tbl)=="table") then
for key, value in pairs(tbl) do
iterate(value, table.extend(parent, key))
end
end
coroutine.yield(parent, tbl)
end
function traverse(root)
return coroutine.wrap(iterate), root
end
Run Code Online (Sandbox Code Playgroud)
然后我意识到我必须使用的Lua环境已coroutine被列入黑名单.我们不能使用它.所以我尝试没有相同的功能coroutine.
-- testdata
local pool = {}
test = {
['a'] = …Run Code Online (Sandbox Code Playgroud) 请告诉我如何以toString功能方式重写.
代码没问题,但没有什么值得骄傲的,其中有3个临时变量.
class Field(x: Int, y: Int) {
val value = init(x,y)
private def init(x: Int, y: Int) = List.fill(x,y)(new Cell)
override def toString(): String = {
val temp = new StringBuilder
for(i <- value) {
for(j <- i) {
temp.append(j.toString())
}
temp.append("\n")
}
temp.mkString
}
}
Run Code Online (Sandbox Code Playgroud)
多谢你们!