Reputation: 838
I have a dataframe below with the column 'samples' that contains lists for each row. I want to turn these into a set. My expected output would be each row in the 'samples' column to not contain any duplicates since it was turned into a set. Any ideas?
import pandas as pd
import numpy as np
df = pd.DataFrame(
{'trial_num': [1, 2, 3, 1, 2, 3],
'subject': [1, 1, 1, 2, 2, 2],
'samples': [list(np.random.randn(3).round(2)) for i in range(6)]
}
)
Output expected is below:
trial_num subject samples
0 1 1 {0.75, 0.87, -0.54}
1 2 1 {-0.67, 1.5, -0.46}
2 3 1 {0.13, -0.56, -0.11}
3 1 2 {-0.78, 0.48, 1.03}
4 2 2 {0.13, 0.62, -0.14}
5 3 2 {0.61, -0.59, 0.43}
Upvotes: 3
Views: 1282
Reputation: 323226
IIUC
df['New']=np.where(df.samples=='','',df.samples.map(set))
df
Out[312]:
trial_num subject samples New
0 1 1 [-0.96, -0.3, 1.03] {-0.96, 1.03, -0.3}
1 2 1 [1.24, -0.04, 0.33] {-0.04, 1.24, 0.33}
2 3 1
3 1 2 [-0.46, 0.1, 1.34] {-0.46, 1.34, 0.1}
4 2 2 [-0.8, -0.63, 0.16] {-0.8, -0.63, 0.16}
5 3 2 [0.88, -0.27, -0.72] {0.88, -0.27, -0.72}
Upvotes: 2