I have a large dataframe with each row containing various amounts of text/string data (song lyrics that were webscraped and then split by line '\n'). Some columns have None values because of this. I'd like to combine all the columns that do have a value into 1 column for each row. I've attached a screenshot so you can see what I'm working with(profanity censored).
1 Answer
To avoid NAs, here's a way using agg
:
df_with_line_sentences.agg(lambda x: ' '.join(i for i in x if not pd.isna(i)), axis=1)
df.stack().groupby(level=0).agg(','.join)
should do the trick