Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find the column number and return it as an array when there is a value in that column in python?

Suppose you have the following data frame:

ID    Q1       Q2      Q3
0     1        0        1
1     0        0        1
2     1        1        1
3     0        1        1  

I would like to return an array with column numbers wherever there is a 1 and add it as another column like this:



ID   Array    Q1       Q2      Q3
0    [0,2]     1        0       1
1    [2]       0        0       1
2    [0,1,2]   1        1       1
3    [1,2]     0        1       1 

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would use numpy.where:

a, b = np.where(df.filter(like='ID')==1)
# or
a, b = np.where(df.drop(columns='ID')==1)

df['Array'] = pd.Series(b).groupby(a).agg(list).set_axis(df.index)

Output:

   ID  Q1  Q2  Q3      Array
0   0   1   0   1     [0, 2]
1   1   0   0   1        [2]
2   2   1   1   1  [0, 1, 2]
3   3   0   1   1     [1, 2]

Pure pandas variant:

df2 = df.filter(like='Q')

df['Array'] = (df2.set_axis(range(df2.shape[1]), axis=1).stack()
                  .loc[lambda s: s==1].reset_index()
                  .groupby('level_0')['level_1'].agg(list)
              )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading