AJD
AJD

Reputation: 90

Add values to a column from a dictionary

I have a dataframe as follows:

df = {'emp': [123, 234], 'state': ['AL', 'CA'], 'start_time': ['08:00', '08:00'], 'end_time': ['17:00', '17:00']
df.head()
emp|state|start_time|end_time
123|AL|11/05/2020 08:00|11/05/2020 17:00
234|CA|11/05/2020 08:00|11/05/2020 17:00

I also have a separate dictionary as follows:

START_ADJUST = {"AL": 0, "CA": 20}

Need a python function that for each state in df adds the number of minutes that is a value for that state key in the dictionary to the value of the military time in 'start_time in the dataframe.

Here's what I tried:

df['prep_mins'] = df['state'].map(START_ADJUST)
df['start_time'] = pd.to_datetime(df['start_time']) + pd.to_timedelta(df['prep_mins'], unit = 'm')

Expected outcome:

emp|state|start_time|end_time
123|AL|11/05/2020 08:00|11/05/2020 17:00
234|CA|11/05/2020 08:20|11/05/2020 17:00

Outcome I'm getting:

emp|state|start_time|end_time
123|AL|11/05/2020 08:00|11/05/2020 17:00
234|CA|11/05/2020 08:00|11/05/2020 17:00

Two One questions:

  1. How do you add minutes to military time?

2) How do add the value of a dictionary value to a column in a data fame?

Upvotes: 0

Views: 50

Answers (1)

jsmart
jsmart

Reputation: 3001

Here is one approach. I added date to original data, and changed the time offset from 0 to 1, to verify that all adjustments get applied.

import pandas as pd

df = {'emp': [123, 234], 
      'state': ['AL', 'CA'], 
      'start_time': ['2020-11-05 08:00', '2020-11-05 08:00'], 
      'end_time':   ['2020-11-05 17:00', '2020-11-05 17:00'],
     }
# create data frame
df = pd.DataFrame(data=df)

# convert data type
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])

# original adjustments
start_adjust = {"AL": 1, "CA": 20}

# convert data type
start_adjust = {
    key: pd.to_timedelta(value, unit='minute')
    for key, value in start_adjust.items()
}

# apply adjustment
df['start_time'] += df.apply(lambda x: start_adjust[x['state']], axis=1)

# results
print(df)

   emp state          start_time            end_time
0  123    AL 2020-11-05 08:01:00 2020-11-05 17:00:00
1  234    CA 2020-11-05 08:20:00 2020-11-05 17:00:00

Upvotes: 1

Related Questions