我想在df列中每个单元格的第n个字符后插入换行符(> br <)。我具有以下功能,但是不知道如何调整它以遍历df列的每一行,而不仅仅是单个文本片段。任何帮助将不胜感激。
def insertNewlines(text, lineLength):
if len(text) <= lineLength:
return text
elif text[lineLength] != ' ':
return insertNewlines(text[:], lineLength + 1)
else:
return text[:lineLength] + '<br>' + insertNewlines(text[lineLength + 1:], lineLength)
响应user2906838:
我的数据包含在名为“消息”的df中,相关列为“消息”。我尝试了您的代码,但是它为该行生成了语法错误:
row[key] = return insertNewlines(text[:], lineLength + 1)
对不起,由于您之前没有发布数据,请在此处找到我的df样本:
Excerpts:
Can this issue be closed?
The other precedences are OK; as far as I can tell... not worth messing around in the code for no demonstrable benefit.
we ran into this issue on our platform. it's possibly very specific to our setup; but the patch should cause no problems for any other setup as well. simplifying this into on line is of course possible. i can't really tell you any more about it; since this issue was fixed in our local implementation about one year ago by some colleague. in preparation for our migration to the current version of shindig and opensocial 0.9 i'm revising all patches and feature additions we did over the last year and will be looking at which are worth comitting. so you can expect some more small issues in the next weeks ;)
Completely non-related and probably belongs in another jira _x0089_ÛÒ but I've added a small patch to fix two comment typos in HttpTransact (I know authorized can be spelt with an s _x0089_ÛÒ but we don't do that anywhere else in the code)
Already fixed in UIMA-1379.
答案 0 :(得分:0)
由于您尚未共享任何数据,因此我只考虑将df
作为您的数据框,该数据框具有所需的任意多文本列。现在,要初始化每一行,您只需使用apply
函数。
changedData = df["Messages"].apply(lambda row:insertNewlines(row), axis=1)
现在,在您的insertNewlines()
方法中,您可以做任何您想做的事情,整行都可以编辑。
def insertNewlines(text, lineLength=int(n)):
if len(text) <= lineLength:
return text
elif text[lineLength] != ' ':
return insertNewlines(text[:], lineLength + 1)
else:
return text[:lineLength] + '<br>' + insertNewlines(text[lineLength + 1:], lineLength)
在这里,这只是您如何执行此操作的示例。希望这可以帮助。