user15551268
user15551268

Reputation: 31

I have a problem with applying my logic into python code

I am new to pandas and I do not know how to apply the following logic into pandas code. All help I can get would be appreciated!

I have, for example, in my dataframe, the following:

id    uid  action program     time         sleepTimes
1628  100   5      200      2020-05-20      2020-05-20
1629  100     1    200      2020-05-21      0

I wanted the column sleepTimes of the id 1629 to have the same value of the column sleepTimes of the id 1628 since they have the same uid and program. I wanted to apply this to all rows that verify the same condition.

For a general perspetive, I wanted to add to the column sleepTimes of action = 1, the values of sleepTimes of action = 5 when actions = 1 have the same value of uid and program of action = 5. How could I do this, using python?

Observation: My dataframe is created using pandas.

Thank you!

EDIT: This is the code I started with.

df['sleepTimes'] = np.where(df['action'] == 5, df['time'], 0)
uid = df.loc[df['action'] == 5, 'uid']

def mapping_time (df):
    if (df['action'] == 1).all():
        #compare and check if uid and program are equal to action 1 and action 5. 
        if df['uid'] == df['sleepTimes'] & df['program']:
            df['sleepTimes'] = df.loc[df['action'] == 5, 'time']
    elif (df['action'] == 3).all():
        if(df['uid'] == uid):
            df['sleepTimes'] = sleepTimes
    else:
#it is action = 5
        df['sleepTimes'] = df['time']

Upvotes: 2

Views: 77

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Just make use of astype() and replace() method:

df['sleepTimes']=df['sleepTimes'].astype(str).replace('0',np.NaN)

Finally make use of groupby() method and ffill() method:

df['sleepTimes']=df.groupby('uid')['sleepTimes'].ffill()

Now if you print df you will get your desired output

#output

    id      uid action  program     time    sleepTimes
0   1628    100     5   200     2020-05-20  2020-05-20
1   1629    100     1   200     2020-05-21  2020-05-20

Now If you want to perform some operations on 'sleepTimes' then convert it to datetime[ns] by using pd.to_datetime() method:

df['sleepTimes']=pd.to_datetime(df['sleepTimes'])

Edit:

df['sleepTimes']=df.groupby(['uid','program'])['sleepTimes'].ffill()

Upvotes: 2

Related Questions