Home > Software engineering >  Create a column with counts of values from multiple columns (if any)
Create a column with counts of values from multiple columns (if any)

Time:10-14

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.

CodePudding user response:

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

CodePudding user response:

Here I am putting all date column values in list and counting zeros in it. like [1,0,2,0,9,1] so 2 time zero and total 6 days so 6-2 = 4 will be No of days.

Code:

df['No_of_Days'] = [len(i[1:]) - i[1:].count('0') for i in df.values.tolist()]

or

df['No_of_Days'] = [len(i[1:]) - i[1:].count('0') for i in df.values.tolist()] if 'No_of_Days' not in df else df['No_of_Days'] 

df

  • Related