0

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).

enter image description here

3
  • df.stack().groupby(level=0).agg(','.join) should do the trick
    – Umar.H
    Commented Feb 27, 2020 at 20:53
  • 1
    Looks like you have to treat this before actually putting it in a data frame. Why do you split by lines in the first place?
    – rafaelc
    Commented Feb 27, 2020 at 20:57
  • 1
    Let's not forget that being "pythonic" also requires the solution be clear and maintainable as well as concise. Exploiting the features of python is great, but if you re-read your code six months from now and wonder "it works, but how?" then you have missed the point.
    – tnknepp
    Commented Feb 27, 2020 at 21:00

1 Answer 1

1

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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.