简体   繁体   English

Python 通过将现有行与列表配对作为值在 dataframe 中创建新列

[英]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.您可以使用来自itertoolscombinations来帮助您解决此问题。

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.

相关问题 如何使用现有列名称和值在Pandas数据框中创建列表的新列? - How to create new column of lists in Pandas dataframe using existing column names and values? 基于现有列的唯一值(列表)的 Pandas DataFrame 中的新列 - new column in pandas DataFrame based on unique values (lists) of an existing column 遍历 pandas 数据框中的行并匹配列表字典中的值以创建新列 - Iterate through rows in pandas dataframe and match values in a dictionary of lists to create a new column 将 dataframe 列值与列表组合以创建新的 dataframe - Combining dataframe column values with lists to create a new dataframe 从列表的python列表和带有列表的列创建新的pandas数据框 - Create a new pandas dataframe from a python list of lists with a column with lists Python-如何从现有列中的唯一值和相应值创建数据框中的新列? - Python - how to create new columns in a dataframe from the unique values from an existing column with corresponding values? Python DataFrame - 删除具有属于值列表的列值的行 - Python DataFrame - deleting rows with column values belonging to lists of values 使用基于现有列和字典的值创建新的数据框列? - Create new dataframe column with values based existing column AND on dictionary? 如何根据列值在现有数据框中添加新行? - How to add new rows in existing dataframe based on the column values? 根据 dataframe 和列表中的现有列创建列 - Create column based on existing column in dataframe and lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM