简体   繁体   English

正则表达式替换使用函数python

[英]regex replace using function python

I'm trying to print a table of my database using : 我正在尝试使用以下方法打印我的数据库表:

pd.read_sql_query("SELECT name,duration FROM activity where (strftime('%W', date) = strftime('%W', 'now'))", conn))

and it work it prints : 它打印工作:

                   name  duration
 0        programmation       150
 1              lecture        40
 2                  ctf        90
 3                  ceh        90
 4        deep learning       133
 5  vm capture the flag       100

but I would like to use my function minuteToStr who translate the duration to string likes "1h30" on the duraton colowns. 但我想使用我的函数minuteToStr将持续时间翻译为duraton colowns上的字符串“1h30”。 I tried this code but it does'nt work : 我试过这段代码,但它不起作用:

tableau = str(pd.read_sql_query("SELECT name,duration FROM activity\
                 where (strftime('%W', date) = strftime('%W', 'now'))", conn))  
tableau = re.sub("([0-9]{2,})",   minuteToStr(int("\\1")), tableau)
print(tableau)

Thanks 谢谢

Make this easy, just use a little mathemagic and string formatting. 使这很简单,只需使用一点mathemagic和字符串格式。

h = df.duration // 60
m = df.duration % 60

df['duration'] = h.astype(str) + 'h' + m.astype(str) + 'm'
df

                  name duration
0        programmation    2h30m
1              lecture    0h40m
2                  ctf    1h30m
3                  ceh    1h30m
4        deep learning    2h13m
5  vm capture the flag    1h40m

re.sub doesn't work this way. re.sub不能这样工作。 It expects a string, not a DataFrame. 它需要一个字符串,而不是DataFrame。

Given that minuteToStr accepts an integer, you can simply use apply : 鉴于minuteToStr接受整数,您只需使用apply

tableau['duration'] = tableau['duration'].apply(minuteToStr)

Similar to using a function inside re.sub in pandas we can use str.replace . 与在pandas中使用re.sub中的函数类似,我们可以使用str.replace Similar type is used here ie 这里使用类似的类型即

If duration column is of integer type then 如果duration列是整数类型则

tableau['duration'].astype(str).str.replace("([0-9]{2,})", minuteToStr)

Else: 其他:

tableau['duration'].str.replace("([0-9]{2,})", minuteToStr)

To illustrate using function inside replace (I prefer you go with @colspeed's solution) 为了说明使用内部替换功能(我更喜欢你使用@ colspeed的解决方案)

def minuteToStr(x):
    h = int(x.group(1)) // 60
    m = int(x.group(1)) % 60
    return str(h) + 'h' + str(m)


df['duration'].astype(str).str.replace("([0-9]{2,})",minuteToStr)
name duration
0     programmation     2h30
1           lecture     0h40
2               ctf     1h30
3               ceh     1h30
4      deeplearning     2h13
5  vmcapturetheflag     1h40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM