Home > Enterprise >  How to add decimal point in front of number in pandas column
How to add decimal point in front of number in pandas column

Time:11-11

I have the following data.

The column ['time_fall_asleep-minute'] represents the minute within the hour that one falls asleep.

The column ['time_fall_asleep-hour'] represents the hour that one falls asleep.

I would like to combine the two columns so that it gives an hour and minute reading.

Thus, the row at index 0 should read as follows (i.e, 12.10 am):

0       0.10

I would be so grateful if anybody could give me a helping hand!

newerdf['time_fall_asleep-minute'] 

0      10.0
1       0.0
2       0.0
3      30.0
5      10.0
       ... 
911    30.0
912    30.0
913    25.0
914    30.0
915    25.0
newerdf['time_fall_asleep-hour'] 

0      0.0
1      2.0
2      6.0
3      0.0
5      0.0
      ... 
911    0.0
912    0.0
913    0.0
914    0.0
915    0.0

CodePudding user response:

You should be able to add them together as strings with a full stop in the middle:

newerdf["time_fall_asleep"] = (
    str(int(newerdf['time_fall_asleep-hour']))
      "." 
      str(int(newerdf['time_fall_asleep-minute']))
)

CodePudding user response:

Pandas has a dedicated Timedelta datatype, specifically for duration.

If you intend to do time-aware computations with this later, you should use this.

You can convert your data as follows:

newerdf["time_fall_asleep"] = (
    pd.to_timedelta(newerdf["time_fall_asleep-minute"], "m")
      pd.to_timedelta(newerdf["time_fall_asleep-hour"], "h")
)
  • Related