[英]Python Pandas replace string based on format
Please, is there any ways to replace "xy" by "x,x+1,x+2,...,y" in every row in a data frame?请问,有什么方法可以将数据框中每一行中的“xy”替换为“x,x+1,x+2,...,y” ? (Where x, y are integer).
(其中 x、y 是整数)。 For example, I want to replace every row like this:
例如,我想像这样替换每一行:
"1-3,7" by "1,2,3,7" “1-3,7”由“1,2,3,7”
"1,4,6-9,11-13,5" by "1,4,6,7,8,9,11,12,13,5" etc "1,4,6-9,11-13,5" 由 "1,4,6,7,8,9,11,12,13,5" 等
I know that by looping through lines and using regular expression we can do that.我知道通过循环遍历行并使用正则表达式我们可以做到这一点。 But the table is quite big and it takes quite some time.
但是桌子很大,需要相当长的时间。 so I think using pandas might be faster.
所以我认为使用熊猫可能会更快。
Thanks alot非常感谢
In pandas you can use apply to apply any function to either rows or columns in a DataFrame.在 Pandas 中,您可以使用apply将任何函数应用于 DataFrame 中的行或列。 The function can be passed with a lambda, or defined separately.
该函数可以通过 lambda 传递,也可以单独定义。
(side-remark: your example does not entirely make clear if you actually have a 2-D DataFrame or just a 1-D Series. Either way, apply
can be used) (旁注:您的示例并没有完全说明您实际上有一个 2-D DataFrame 还是只有一个 1-D 系列。无论哪种方式,都可以使用
apply
)
The next step is to find the right function.下一步是找到正确的函数。 Here's a rough version (without regular expressions):
这是一个粗略的版本(没有正则表达式):
def make_list(str):
lst = str.split(',')
newlst = []
for i in lst:
if "-" in i:
newlst.extend(range(*[int(j) for j in i.split("-")]))
else:
newlst.append(int(i))
return newlst
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.