简体   繁体   中英

Iterate through multiple columns in a Panda dataframe and find count unique values

I am working with a dataset which looks like below:

在此处输入图像描述

I have imported this dataset to my code using the panda library. My goal is to find unique entries of the programming languages from columns 2, 3, 4. I wish the output to be:

    Python 4
    Perl 3
    C++ 3
....

Any leads would be helpful

Use DataFrame.filter with DataFrame.stack and Series.value_counts :

s = df.filter(like='Language').stack().value_counts()

This is an alternative way

df['lang1'].value_counts() + df['lang2'].value_counts() + df['lang3'].value_counts()

or

cols = ['lang1', 'lang2', 'lang2']
sum([df[col].value_counts() for col in cols])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM