简体   繁体   English

如何制作一个 function 将遍历 pandas dataframe 中的列并返回唯一值

[英]How to make a function that will iterate through the columns in a pandas dataframe and return unique values

training_data = [
    ['Green',3,'Apple'],
    ['Yellow',3,'Apple'],
    ['Red',1,'Grape'],
    ['Red',1,'Grape'],
    ['Yellow',3,'Lemon']
]
def unique_values(df,col):
     return set([row[col] for row in df])

unique_values(training_data,1)

output = {1,3}

I want to be able to do this but with a pandas data frame instead of a list我希望能够做到这一点,但使用 pandas 数据框而不是列表

Like this?像这样?

>>> import pandas as pd
>>> training_data = [
...     ['Green',3,'Apple'],
...     ['Yellow',3,'Apple'],
...     ['Red',1,'Grape'],
...     ['Red',1,'Grape'],
...     ['Yellow',3,'Lemon']
... ]
>>> df = pd.DataFrame(training_data, columns = ['color', 'number', 'fruit'])
>>> df.head()
    color  number  fruit
0   Green       3  Apple
1  Yellow       3  Apple
2     Red       1  Grape
3     Red       1  Grape
4  Yellow       3  Lemon
>>> df.number.unique()
array([3, 1])

You can use Series.unique to find unique values in a column.您可以使用Series.unique在列中查找唯一值。

Create a dataframe from your list like this:从您的列表中创建一个 dataframe,如下所示:

In [1974]: import pandas as pd

In [1975]: df = pd.DataFrame(training_data, columns = ['color', 'number', 'fruit'])

In [1986]: df
Out[1986]: 
    color  number  fruit
0   Green       3  Apple
1  Yellow       3  Apple
2     Red       1  Grape
3     Red       1  Grape
4  Yellow       3  Lemon

Then have your function like this:然后让你的 function 像这样:

In [1983]: def unique_values(df,col):
      ...:     return df[col].unique().tolist()
      ...: 

Run your function like this:像这样运行你的 function:

In [1988]: unique_values(df, 'color')
Out[1988]: ['Green', 'Yellow', 'Red']

In [1989]: unique_values(df, 'fruit')
Out[1989]: ['Apple', 'Grape', 'Lemon']

In [1990]: unique_values(df, 'number')
Out[1990]: [3, 1]
def unique_values(df,column):
        return set(df[column])

This also worked after turning the list into a data frame, thank you both!这在将列表变成数据框后也有效,谢谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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