Home > Net >  I want to create a string from the first word of each tuple in a list of tuples
I want to create a string from the first word of each tuple in a list of tuples

Time:08-09

I have the following dataframe

data = [[1,[('car', 'NN'), ('park', 'VB'), ('in', 'PRP'), ('lobby', 'NN')]], [0, [('Dany', 'NN'), ('has', 'VB'), ('an', 'CC'), ('apple', 'NN')]]]
df = pd.DataFrame(data, columns = ['sen', 'col'])

Expected Output:

sen col
1 "car park in lobby"
2 "Dany has an apple"

CodePudding user response:

data = [[1,[('car', 'NN'), ('park', 'VB'), ('in', 'PRP'), ('lobby', 'NN')]], [2, [('Dany', 'NN'), ('has', 'VB'), ('an', 'CC'), ('apple', 'NN')]]]
df = pd.DataFrame(data, columns = ['sen', 'col'])
df.col = df.col.apply(lambda l: ' '.join([e[0] for e in l]))
print(df)

prints

index sen col
0 1 car park in lobby
1 2 Dany has an apple
  • Related