重塑从长到宽格式[重复](Reshape from long to wide format [duplicate])
这个问题在这里已经有了答案:
- 如何将数据从长格式转换为宽格式? 8个答案
例
customer_code items 1 sugar 1 salt 2 sugar 2 accessories 3 salt
期望的输出
customer_code item item2 item3 1 sugar salt 2 sugar accessories 3 salt
This question already has an answer here:
- How to reshape data from long to wide format 11 answers
example
customer_code items 1 sugar 1 salt 2 sugar 2 accessories 3 salt
Desired output
customer_code item item2 item3 1 sugar salt 2 sugar accessories 3 salt
原文:https://stackoverflow.com/questions/30392810
更新时间:2021-03-26 10:03
最满意答案
要捕捉所有到下一个问题的句子,以非贪婪的方式(
(.*)?
)跟随问题或文本结尾((?=^(.*?)\?$|\Z)
)。 由于它是零长度断言,所以后者应该使用积极的后台处理进行匹配。(?ms)(^[^.?!]+\?)(.*?)(?=^(.*?)\?$|\Z)
To capture all sentences up to the next question match any text after a question in non-greedy fashion (
(.*)?
) followed by a question or end of text ((?=^(.*?)\?$|\Z)
). The latter should be matched using positive look-behind since it's a zero-length assertion.(?ms)(^[^.?!]+\?)(.*?)(?=^(.*?)\?$|\Z)
相关问答
更多-
试试这段代码: String str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + "My second question? Sed amet elementum." + "Integer nec diam erat, eu consectetur nibh?" + "Cum sociis natoque penatibus et magnis di ...
-
看一下例子。 old_line = "" with open("in.txt", "r") as f, open("out.txt", "w") as h: for ind,l in enumerate(f): l = l.strip() line = l.lower() if (len(line) < 2): continue if line.startswith("a."): if(ind > 2): ...
-
使用正则表达式从文本文件(python)中获取文件夹的标题(using a regex to grab titles of folders from text file (python))[2021-07-12]
您的代码存在许多错误和潜在的改进。 它们在散文中不容易解释,所以这里是代码的工作版本,其中的注释突出了它们背后的变化和原因。 import os import re # Precompile the regex so it only happens once. This saves a bit of time, # especially if your file is large. # I've also modified the regex to include a capture group [1] ... -
要捕捉所有到下一个问题的句子,以非贪婪的方式( (.*)? )跟随问题或文本结尾( (?=^(.*?)\?$|\Z) )。 由于它是零长度断言,所以后者应该使用积极的后台处理进行匹配。 (?ms)(^[^.?!]+\?)(.*?)(?=^(.*?)\?$|\Z) 演示: https : //regex101.com/r/yEOtAa/1 To capture all sentences up to the next question match any text after a question in n ...
-
很可能是Wordpad作为空格显示的字符实际上是非ASCII字符,或者该文件使用的是您在C#程序中不期望的编码。 在十六进制编辑器中查看该文件。 如果您没有方便的十六进制编辑器,则可以使用File.ReadAllBytes将文件作为字节数组读入,并查找在您看到问号的位置附近的可打印ASCII值范围之外的字节值。 It could very well be that the characters displayed by Wordpad as spaces are in fact non-ASCII char ...
-
我不明白为什么你需要一个正则表达式。 怎么样... from collections import Counter titles = [] for line in salary_file.readlines(): line.rstrip() (campus, name, title) = line.split('\t') titles.append(title) print "\nMost common job titles\n-------" for i, (title, ...
-
正则表达式匹配包含数字但不以问号结尾的文本(Regex to match text containing a digit but not ending with a question mark)[2022-11-25]
^(?!.*?\\?).*\\d.*$ 试试这个。这会找不到句子? 和数字。 ^(?!.*?\\?).*\\d.*$ Try this.This will find sentences without ? and with digits. -
使用正则表达式将文本拆分为标记时保留特殊标记(Keeping special marks when splitting text into tokens using regex)[2022-11-13]
尝试这个: token_pattern = re.compile(r"(?u)[^\w ]|\b\w+\b") token_pattern.findall(text) 它也匹配所有非字母数字字符作为单个匹配。 如果你真的只需要问号和感叹号,你可以改变正则表达式 token_pattern = re.compile(r"(?u)[!?]|\b\w+\b") token_pattern.findall(text) Try this: token_pattern = re.compile(r"(?u)[^\w ... -
预测不会使正则表达式指数向前移动,它“坚持立场”,但它需要在字符串中的当前位置之后存在或不存在某些模式。 当你使用(?=1)x ,你告诉正则表达式引擎: 下一个字符必须是1 在这个位置,匹配字符x 。 这意味着你需要x为1 ,它永远不会是真/永远是假的。 这个正则表达式永远不会匹配任何东西。 这是regular-expressions.com的另一个例子: 让我们应用q(?=u)i来quit 。 前瞻现在是积极的,后面是另一个令牌。 同样, q匹配q , u匹配u 。 同样,必须丢弃前瞻的匹配,因此引擎从字 ...
-
好吧,首先,您可以通过将分组更改为以下内容来使正则表达式更快一些: @"^\s*[0-9A-Fa-f]{1,2}(,\s*[0-9A-Fa-f]{1,2}\s*)*$" 现在,如果你想允许*和? 同样,你可以使用这样的东西: @"^\s*([0-9A-Fa-f]{1,2}|\*\*?|\?\??)(,\s*([0-9A-Fa-f]{1,2}|\*\*?|\?\??)\s*)*$" 我基本上将[0-9A-Fa-f]{1,2}更改为([0-9A-Fa-f]{1,2}|\*\*?|\?\??) 。 您也可以使 ...