[英]Python create new column in dataframe by pairing existing rows with lists as values
I currently have a dataframe of lists that looks something like this.我目前有一个看起来像这样的列表的 dataframe。
Index指数 | Value价值 |
---|---|
1 1 | A, B甲,乙 |
2 2 | C C |
3 3 | D D |
I would like to create a new column that looks something like this:我想创建一个看起来像这样的新列:
Index指数 | Value价值 | Value_y值_y |
---|---|---|
1 1 | A,B甲,乙 | C C |
2 2 | A,B甲,乙 | D D |
3 3 | C C | D D |
For some reason, I was unable to place the square parenthesis in the columns.出于某种原因,我无法将方括号放在列中。 But they are lists.但它们是列表。
Essentially, I would like the new dataframe to have values as unique pairs.本质上,我希望新的 dataframe 具有作为唯一对的值。 I understand that there is a way to do this if the columns do not contain lists as values, but since my values are lists, they are unhashable.我知道如果列不包含列表作为值,有一种方法可以做到这一点,但由于我的值是列表,它们是不可散列的。 Is there a way to do this should the columns contain lists?如果列包含列表,有没有办法做到这一点? Thanks in advance!提前致谢!
You can use combinations
from itertools
to help you with this.您可以使用来自itertools
的combinations
来帮助您解决此问题。
from itertools import combinations
data = {'value':[['A','B'],['C'],['D']]}
df = pd.DataFrame(data)
new_df = pd.DataFrame(list(combinations(df.value.tolist(), 2)), columns = ['Value', 'Value_y'])
new_df
Out[57]:
Value Value_y
0 [A, B] [C]
1 [A, B] [D]
2 [C] [D]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.