Pandas DataFrames中不同的组合值 [英] Distinct combinations values in Pandas DataFrames

查看:68
本文介绍了Pandas DataFrames中不同的组合值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来提取数据帧中值的不同组合?我已经将pd.Series.unique()用于单列,但是多列呢?

Is there an easy way to pull out the distinct combinations of values in a dataframe? I've used pd.Series.unique() for single columns, but what about multiple columns?

示例数据:

df = pd.DataFrame(data=[[1, 'a'], [2, 'a'], [3, 'b'], [3, 'b'], [1, 'b'], [1, 'b']], 
                  columns=['number', 'letter'])

Expected output:
(1, a)
(2, a)
(3, b)
(1, b)

理想情况下,我想要一个具有不同值的元组的单独Series对象.

Ideally, I'd like a separate Series object of tuples with the distinct values.

推荐答案

您可以压缩列并创建集合:

You can zip the columns and create a set:

>>> set(zip(df.number, df.letter))
{(1, 'a'), (1, 'b'), (2, 'a'), (3, 'b')}

这篇关于Pandas DataFrames中不同的组合值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆