[scikit-learn] Identifying column names of Non-zero values

Maciek Wójcikowski maciek at wojcikowski.pl
Wed Oct 5 07:53:28 EDT 2016


Hi Sanant and Samo,

Even easier and faster solution:

> df.columns[(df.values != 0).any(axis=0)]

Or if some reason != 0 does not work for you:

> df.columns[(~(df.values == 0)).any(axis=0)]

----
Pozdrawiam,  |  Best regards,
Maciek Wójcikowski
maciek at wojcikowski.pl

2016-10-05 13:35 GMT+02:00 Samo Turk <samo.turk at gmail.com>:

> Something like this might work:
>
> def non_zero(row, columns):
>     return list(columns[~(row == 0)])
>
> df.apply(lambda x: non_zero(x, df.columns), axis=1)
>
> Cheers,
> Samo
>
> On Wed, Oct 5, 2016 at 11:58 AM, Startup Hire <blrstartuphire at gmail.com>
> wrote:
>
>> Hi Pypers,
>>
>> Hope you are doing well.
>>
>> I am working on a project to find out the column names of non-zero values
>> at a row level.
>>
>> How can this effectively done in python pandas/dataframe?
>>
>>
>> For example,
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *Column1* *Column *2 *Column *3 Column 4 Column 5 Column 6 *Column 7* New
>> column to be created
>> 1 1 1 0 0 0 1 Column1,Column 2,Column 3,Column7
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> I might have to do it on approximately million rows
>>
>> Regards,
>> Sanant
>>
>> _______________________________________________
>> scikit-learn mailing list
>> scikit-learn at python.org
>> https://mail.python.org/mailman/listinfo/scikit-learn
>>
>>
>
> _______________________________________________
> scikit-learn mailing list
> scikit-learn at python.org
> https://mail.python.org/mailman/listinfo/scikit-learn
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20161005/c575c862/attachment-0001.html>


More information about the scikit-learn mailing list