简体   繁体   中英

What is the most pythonic way to combine multiple string columns to create a new Pandas series?

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

在此处输入图片说明

为了避免 NAs,这里有一种使用agg的方法:

df_with_line_sentences.agg(lambda x: ' '.join(i for i in x if not pd.isna(i)), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM