Python create new column in dataframe by pairing existing rows with lists as values
Question:
I currently have a dataframe of lists that looks something like this.
Index
Value
1
A, B
2
C
3
D
I would like to create a new column that looks something like this:
Index
Value
Value_y
1
A,B
C
2
A,B
D
3
C
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. 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!
Answers:
You can use combinations
from itertools
to help you with this.
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]
I currently have a dataframe of lists that looks something like this.
Index | Value |
---|---|
1 | A, B |
2 | C |
3 | D |
I would like to create a new column that looks something like this:
Index | Value | Value_y |
---|---|---|
1 | A,B | C |
2 | A,B | D |
3 | C | 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. 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.
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]