in Education by
I have a dataset with employee payroll information (df2). It has a date, job title, shift start time, hours worked. The goal is to create a dataset (df) which shows how many employees were working at any given hour. The problem I am facing is that setting the value in a column is not having any effect on the original dataset (df). data1 = [['2/1/2019','Cashier',0,0,0,0,0,0,0], ['2/2/2019','Cashier',0,0,0,0,0,0,0], ['2/1/2019','Server',0,0,0,0,0,0,0]] cols1 = ['Date', 'Job'] + list(pd.date_range(pd.to_datetime('2/1/2019 15:00'), periods=7, freq='H')) df=pd.DataFrame(data1, columns=cols1) data2=[['2/1/2019', 'Cashier', pd.to_datetime('2/1/2019 16:00'), 5.2]] cols2=['Date', 'Job', 'Start', 'Hours'] df2=pd.DataFrame(data2, columns=cols2) def count_shifts(x): start_time=x['Start'] worked_in_minutes =round(x['Hours']) * 60 + (x['Hours'] - round(x['Hours'])) worked_range_index = pd.date_range(start_time, periods=worked_in_minutes, freq='T') worked_series = pd.Series(1/60, index=worked_range_index) worked_series=worked_series.resample('H', label='left').sum() df.loc[:,list(worked_series.index)] \ [(df['Job']==x['Job']) & (df['Date']==x['Date'])] = worked_series.values df2.apply(count_shifts, axis=1) I expect df columns corresponding to the hours 15:00,16:00,17:00,18:00,19:00 to equal 1 and 20:00 to equal .2 But the actual result is that they are still 0 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
There are two issues: First worked_in_minutes =round(x['Hours']) * 60 + (x['Hours'] - round(x['Hours'])) is not doing what you expect it to do. It equals 300.2 for the first row in df2 instead of 312 which is what you might be expecting. There is no point in separating out the hours/minutes since it's already in decimal. worked_in_minutes = round(x['Hours'] * 60 should suffice. Second, your assigning statement is first getting a subset and then setting something. This can have unexpected behavior. Change it to df.loc[(df['Job']==x['Job']) & (df['Date']==x['Date']),list(worked_series.index)] = worked_series.values

Related questions

0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have a simple method to search a pandas dataframe column for a list of keywords; however, I'd like to create a ... do everyth 28,passei o dia com o meu amor comemo demai...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    Say I have a dictionary that the key of year, and the corresponding value of a list of values. Is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have a csv file and I am reading this file in python using pandas. I want to read each row of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    18F-AV-1451-A07 Value refer to another sheet called "CONTENT" in which column "B" and row "3". ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a pandas series containing a list of dictionaries. I'd like to parse the contents of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I have a dataset |category| cat a cat b cat a I'd like to be able to return something like (showing unique values ... cat a 2 cat b 1 Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
...