-1

I have two columns hours and minutes stored separately in columns: a and b I want to calculate the sum of both in terms of minutes(DURATION)

To convert the hour into minutes I have used the following:

        train['duration']=train.a.apply(lambda x:x*60)

Now I want to add the minutes to the newly created duration column.

        So that my final value is duration=(a*60)+b

I am unable to perform this operation using lambda and for loop takes forever to execute In pandas.

  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on how to ask a good question may also be useful. – yatu Apr 06 '20 at 14:41

3 Answers3

0

Pandas is a very powerful module with neat features. If you want to simply add values of 1 column to values in another respectively you can simply do + operation on the columns. Here is my example:

import pandas as pd

df = pd.DataFrame({"col1":[1,2,3,4], "col2":[10,20,30,40]})

df["sum of 2 columns"] = df["col1"] + df["col2"]

print(df)

And this is the output:

   col1  col2  sum of 2 columns
0     1    10                11
1     2    20                22
2     3    30                33
3     4    40                44
Karl Olufsen
  • 186
  • 1
  • 10
0

You can do it using lambda as follows.

import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
df["sum"] = df.apply(lambda row: row.x + row.y, axis=1)
print(df)

It will give following output:

   x  y  sum
0  1  4    5
1  2  5    7
2  3  6    9

Hope it helps. You can do it using List Comprehension as that will be fast also or as @Karl Olufsen suggested.

Rajnish kumar
  • 186
  • 1
  • 14
0

Best would be to use vectorized code which would be faster than apply

df['duration_in_min'] = df['hour'] * 60 + df['min']

The time complexity of various operations from best(fastest) to worst(slowest) is:

Cython procedures < Vectorized code < apply < itertuples < iterrows

Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55