I have a dataframe with date columns:``
import pandas as pd
df = pd.DataFrame({'_id':['633739b043e8750c660feabd'] + ['63373966bf5eb50c6d593b1f'] + ['633738aa43e8750c660fe90e'] + ['63373853bf5eb50c6d5938c1'],
'2022-10-01':['1', '0', '0', '2'],
'2022-10-02':['0', '1', '0', '0'],
'2022-10-03':['2', '8', '3', '9'],
'2022-10-04':['0', '0', '0', '2'],
'2022-10-05':['9', '0', '0', '2'],
'2022-10-06':['1', '0', '3', '2'],
})
I need to get the counts in another column if the Id's date column has a value in it.
The result should look like this:
import pandas as pd
df = pd.DataFrame({'_id':['633739b043e8750c660feabd'] + ['63373966bf5eb50c6d593b1f'] + ['633738aa43e8750c660fe90e'] + ['63373853bf5eb50c6d5938c1'],
'2022-10-01':['1', '0', '0', '2'],
'2022-10-02':['0', '1', '0', '0'],
'2022-10-03':['2', '8', '3', '9'],
'2022-10-04':['0', '0', '0', '2'],
'2022-10-05':['9', '0', '0', '2'],
'2022-10-06':['1', '0', '3', '2'],
'No_of_Days':['4', '2', '2', '5']
})
Sorry the image was not being uploaded due to server error So I have added a sample codes. It's simple but still I am struggling as I am new to this.
You can set the index as _id
, then convert the rest of the df to integer and take the row-wise sum of values greater than zero.
df['No_of_Days'] = df.set_index('_id').astype(int).gt(0).sum(1).values