简体   繁体   中英

Take all unique values from certain columns in pandas dataframe

I have a simple question for style and how to do something correctly.

I want to take all the unique values of certain columns in a pandas dataframe and create a map ['columnName'] -> [valueA,valueB,...]. Here is my code that does that:

listUnVals = {}

for col in df: 
    if ((col != 'colA') and (col != 'colB')):
        listUnVals[col] = (df[col].unique()) 

I want to exclude some columns like colA and colB. Is there a better way to filter out the columns I don't want, except writing an if (( != ) and ( != ...) . I hoped to create a lambda expression that filters this values but I can't create it correctly.

Any answer would be appreciated.

Couple of ways to remove unneeded columns

df.columns[~df.columns.isin(['colA', 'colB'])]

Or,

df.columns.difference(['colA', 'colB'])

And, you can ignore loop with

{c: df[c].unique() for c in df.columns[~df.columns.isin(['colA', 'colB'])]}

You can create a list of unwanted columns and then check for in status

>>> unwanted = ['columnA' , 'columnB']
>>> for col in df:
        if col not in unwanted:
            listUnVals[col] = (df[col].unique()) 

Or using dict comprehension:

{col : df[col].unique() for col in df if col not in unwanted}

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