First two rows of a dataframe, df
:
0|50331648|{1,2,3,4,5}|6
1|50331649|{3,5,7,8}|2
After performing the operation, I just need a set that contains {1,2,3,4,5,7,8}
.
How to achieve it?
To merge two pandas DataFrames on multiple columns use pandas. merge() method. merge() is considered more versatile and flexible and we also have the same method in DataFrame.
You can replace all values or selected values in a column of pandas DataFrame based on condition by using DataFrame. loc[] , np. where() and DataFrame.
Assuming "B"
to be the column name under consideration, you could use set.union
on the obtained unpacked list:
set.union(*df['B'].tolist())
{1, 2, 3, 4, 5, 7, 8}
(Or)
Supply these as a callable function to reduce
:
from functools import reduce # If you're on Py3k
reduce(set.union, df['B'].tolist())
{1, 2, 3, 4, 5, 7, 8}
Data:
df = pd.DataFrame(dict(A=[50331648, 50331649],
B=[{1,2,3,4,5}, {3,5,7,8}],
C=[6,2])
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With