I have a df with a column like this:
words
1 ['me']
2 ['they']
4 ['it', 'we', 'it']
5 []
6 ['we', 'we', 'it']
I want it to look like this:
words
1 'me'
2 'they'
4 'it we it'
5 ''
6 'we we it'
I have tried both these options, but they both yield in a result identical to the original series.
def join_words(df):
words_string = ''.join(df.words)
return words_string
master_df['words_string'] = master_df.apply(join_words, axis=1)
and...
master_df['words_String'] = master_df.words.str.join(' ')
Both these result in the original df. What am I doing wrong?
Edit
Using master_df['words_string'] = master_df['words'].apply(' '.join), I got:
1 [ ' m e ' ]
2 [ ' t h e y ' ]
4 [ ' i t ' , ' w e ' , ' i t ' ]
5 [ ]
6 [ ' w e ' , ' w e ' , ' i t ' ]
master_df.words.str.join(' ')should work, checkast.literal_evalif they are just the string repr of a list , its better to includedf.head().to_dict()in your question toodf['words'].apply(literal_eval).agg(' '.join)if it's a list not a string