Home > Net >  Convert List of String Sets in Column to New Column
Convert List of String Sets in Column to New Column

Time:12-15

df.x



0                                                   []
1    [{cat=1, data=adjks}, {cat=1, data=pqoek}, {cat=2, data=hjksy}]
2                                                   []
3    [{cat=1, data=alpqi}]
4    [{cat=5, data=weee}, {cat=6, data=wolpwolp}]
Name: x, dtype: object

I have a column where ea. row is a list of sets. I am only interested in sets where cat=1 though.

What would be the best way to iterate over this column for ea. row and extract the cat=1 'data' values in a new column?

What I'd hope to get

  new_column
0 
1 adjks, pqoek
2 
3 alpqi
4

CodePudding user response:

If it's a list of strings then we can apply a function that works directly work on sets:

out = df['x'].apply(lambda lst: ', '.join(tuple(s-set(['cat=1']))[0].split('=')[1] for s in lst if 'cat=1' in s))

Output:

0                
1    adjks, pqoek
2                
3           alpqi
4                
dtype: object

CodePudding user response:

# import Pandas library
import pandas as pd

# create dataframe with a column (names) having list-like elements
data = {'id': [1, 2, 3],
        'names': ["Tom,Rick,Hardy", "Ritu,Shalini,Anjana", "Ali,Amir"]}

df = pd.DataFrame(data)

print(df)
  • Related