0

I have a dataframe with minutes as an int in one col and seconds as a float in another column. I haven't been able to combine them so I want to create an object col to display them where I use an f-string to concatenate the minutes with the seconds and but a ":" in between

new_df = pd.DataFrame()
new_df['minutes'] = minutes
new_df['seconds'] = seconds

new_df

    minutes seconds
0   4   49.3
1   4   55.8
2   4   56.9
3   5   2.9
4   5   4.2
new_df['display_time'] = (f'{new_df["minutes"]}:{new_df["seconds"]}')
new_df



minutes seconds display_time
0   4   49.3    0 4\n1 4\n2 4\n3 5\n4 ...
1   4   55.8    0 4\n1 4\n2 4\n3 5\n4 ...
2   4   56.9    0 4\n1 4\n2 4\n3 5\n4 ...
3   5   2.9 0 4\n1 4\n2 4\n3 5\n4 ...
4   5   4.2 0 4\n1 4\n2 4\n3 5\n4 ...
... ... ... ...
615 8   12.7    0 4\n1 4\n2 4\n3 5\n4 ...
616 8   17.7    0 4\n1 4\n2 4\n3 5\n4 ...
617 8   24.4    0 4\n1 4\n2 4\n3 5\n4 ...
618 8   27.2    0 4\n1 4\n2 4\n3 5\n4 ...
619 8   36.5    0 4\n1 4\n2 4\n3 5\n4 ...

My output keeps looking like this and I can't figure out how to make it look like

4:49.3
4:55.8
4:56.9
5:02.9
5:04.9

2 Answers2

1

You can simply convert each column into a str and concatenate them like this:

new_df['display'] = new_df['minutes'].astype(str) + ":" + new_df['seconds'].astype(str)
P. Shroff
  • 396
  • 3
  • 5
1

You can convert both columns as string then use str accessor:

new_df['display_time'] = new_df['minutes'].astype(str).str.cat(
                             new_df['seconds'].astype(str).str.zfill(4), sep=':')

Output:

>>> new_df
   minutes  seconds display_time
0        4     49.3       4:49.3
1        4     55.8       4:55.8
2        4     56.9       4:56.9
3        5      2.9       5:02.9
4        5      4.2       5:04.2
Corralien
  • 109,409
  • 8
  • 28
  • 52