python - 从 Pandas Column 获取每行一个值的列表

标签 python pandas list

Example of my Dataframe

我想把'column of text'列变成一个列表。如何将单元格内的每个新行转换为列表的值。我通常会使用类似的东西:

my_new_list = df.Column_of_text.tolist()

问题是我需要每个单元格内的每一行都在我的新列表中有一个值。

我期望的输出示例:

my_new_list =["Hey how is it going","good how are youI am fine thank y...", "Nice Weather today, eh?", etc...]

对此有什么想法吗?这将不胜感激。

我注意到这已被标记为可能重复,但事实并非如此。我的问题与获取专栏中最流行的词无关......

最佳答案

我会使用 chain.from_iterable + str.split('\n')

import itertools

my_list = list(itertools.chain.from_iterable(df['column_name'].str.split('\n')))

列表理解在这里也是一个好主意

list(itertools.chain.from_iterable([text.split('\n') for text in df['column_name']]))

关于python - 从 Pandas Column 获取每行一个值的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58347619/

相关文章:

python - 根据日期、周、月获取平均数据

java - 链表排序问题

delphi - 向 TList 和 TStringList 添加稳定排序的简单方法

python - 为什么 readline.read_history_file 给我 'IOError: [Errno 2] No such file or directory'

Python通过大于或等于每个子组中值的列值查找数据框中的记录

python - 避免遍历 Pandas 中的每一行以进行自定义聚合

python - 如何根据列中唯一值的数量从字典中删除/删除数据帧?

C 判断链表中是否存在某个值

python - 名称错误 : global name 'execfile' is not defined trying to run an app on Google App Engine Launcher

python - pygame连续和同时按键输入