python:两列的组合 [英] python: Combination of two Columns

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

问题描述

我已使用以下代码从一个数据帧中隔离出一列:

I have isolated a column from one dataframe, using the code:

 Column_a = df1.loc[:,'Column_a_Name']

和另一个数据框的第二列,等效地使用:

and a second column from another dataframe, equivalently using:

 Column_b = df2.loc[:,'Column_b_Name'].

这些列包含名称,我想创建一个列表,列出每个名称中两个名称的所有可能组合.例如:

These columns contain names, I would like to create a list of all possible combinations of the two names in each. For example:

 Column_a         Column_b
 Adam             Smith
 Barry            Brown
 Ben              Red

我想要实现的结果是一个自然的数据框

The result I am trying to achieve is a dataframe of the nature

 [(Adam,Smith), (Adam, Brown), (Adam,Red), (Barry, Brown),...,(Ben, Red)]

我已经尝试了有用的功能itertools.combinations(Column_a,Column_b),但这只会返回结果:TypeError:无法将系列转换为<输入"int">.谢谢

I have tried the useful function itertools.combinations (Column_a, Column_b), but this just returns the result: TypeError: cannot convert the series to < type 'int' >. Thanks

推荐答案

使用 itertools.product

>>>>df = pd.DataFrame(data=[['Adam', 'Smith'], ['Barry', 'Brown'], ['Ben', 'Red']], columns=['Column_a_Name', 'Column_b_Name'])
df

  Column_a_Name Column_b_Name
0          Adam         Smith
1         Barry         Brown
2           Ben           Red

>>>>from itertools import product

>>>>list(product(df['Column_a_Name'], df['Column_b_Name']))


 [('Adam', 'Smith'),
 ('Adam', 'Brown'),
 ('Adam', 'Red'),
 ('Barry', 'Smith'),
 ('Barry', 'Brown'),
 ('Barry', 'Red'),
 ('Ben', 'Smith'),
 ('Ben', 'Brown'),
 ('Ben', 'Red')]

注意:product函数返回一个生成器.如果要遍历数据,则不需要列表.

Note: The product function returns a generator. If you want to loop over the data, you don't need a list.

这篇关于python:两列的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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