简体   繁体   English

组合多个字符串列以创建新的 Pandas 系列的最pythonic 方法是什么?

[英]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').我有一个大数据框,每行包含不同数量的文本/字符串数据(被网络抓取然后按行“\\n”分割的歌词)。 Some columns have None values because of this.因此,某些列具有 None 值。 I'd like to combine all the columns that do have a value into 1 column for each row.我想将所有具有值的列合并为每行的 1 列。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Pandas中创建许多新列的大多数Pythonic方法 - Most Pythonic Way to Create Many New Columns in Pandas 应用函数和返回多列的最pythonic方法是什么? - What is the most pythonic way to apply a function on and return multiple columns? 从字符串中删除字符并创建子字符串的最pythonic方法是什么? - What is the most pythonic way to remove characters from a string and create substrings? 什么是逻辑上结合布尔列表的最“pythonic”方式? - What is the most 'pythonic' way to logically combine a list of booleans? Pythonic方式过滤列然后创建新列 - Pythonic way to filter columns and then create a new column Pandas - 将多行合并为一行并创建新列 - Pandas- Combine multiple rows into a single row and create new columns 重新编写大熊猫列的最有效和pythonic方法是什么? - What is the most efficient & pythonic way to recode a pandas column? 在 pandas 中计算 limsup/liminf 的最有效和 Pythonic 方法是什么? - What is the most efficient and Pythonic way of calculating limsup/liminf in pandas? 熊猫:通过汇总不同df的列来创建新df的Python方法 - Pandas: Pythonic way to create new df by summing columns from different dfs 在具有多种类型的空白字符的字符串中的每个单词上应用函数的最有效的方法是什么? - What's the most pythonic way to apply a function on every word in a string with multiple types of white space characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM