2

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. 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

1
  • Can you add your function? Commented Nov 5, 2017 at 8:44

3 Answers 3

3

Make this easy, just use a little mathemagic and string formatting.

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
Sign up to request clarification or add additional context in comments.

4 Comments

This is certainly the easiest way of doing it.
@Bharath Passed you an upvote too, and the answer you linked ;-)
Thank you.My solution might help future users who has duration like 120 mins or so.
@Neolex Just letting you know that apply is much slower.
2

re.sub doesn't work this way. It expects a string, not a DataFrame.

Given that minuteToStr accepts an integer, you can simply use apply:

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

1 Comment

Thanks for the help :) I'll use this solution .
2

Similar to using a function inside re.sub in pandas we can use str.replace . Similar type is used here i.e

If duration column is of integer type then

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)

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

3 Comments

Thank's for your help, but it doesnt work ... I got : ` name duration 0 programmation NaN 1 lecture NaN 2 ctf NaN 3 ceh NaN 4 deep learning NaN 5 vm capture the flag NaN 6 programmation NaN `
Its better you provide the function minuteToStr in the question.
@Neolex check the update this is how you should use function inside replace. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.