简体   繁体   English

如何将列字符串值分解为单个单词? 每个值由多个字符串值组成。 早期学习者在这里

[英]How can I break column string values into individual words? Each value consists of multiple string values. Early learner here

I imported a dataframe to Pycharm and want to eventually count the number of occurrences a value appears using for loops (practicing loops now as an early learner).我将一个数据框导入到 Pycharm,并希望最终使用 for 循环计算一个值出现的次数(现在作为早期学习者练习循环)。 I imported the dataframe and converted the column I'm seeking to write a for loop into a list.我导入了数据框并将我要编写 for 循环的列转换为列表。 Since the column values were recognized as a float, I converted the values to strings.由于列值被识别为浮点数,因此我将这些值转换为字符串。

Right now, I'm trying to split the values into individual words but nothing is changing.现在,我正在尝试将值拆分为单个单词,但没有任何变化。 If someone could guide me through this problem, would be greatly appreciated.如果有人可以指导我解决这个问题,将不胜感激。 Please see the code below:请看下面的代码:

INPUT:输入:

lsti = df.Industries.tolist()
for value in lsti:
    value = str(value)
    word = value.split(',')
print(lsti)

OUTPUT I'M GETTING:我得到的输出:

['Artificial Intelligence, Cloud Security, Cyber Security, Risk Management']...

DESIRED OUTPUT I WANT:我想要的期望输出:

['Artificial Intelligence', 'Cloud Security', 'Cyber Security', 'Risk Management']...

Well your issue here is that you are actually converting your data frame to list and them running a loop.那么你的问题是你实际上正在将你的数据框转换为列表并且它们运行一个循环。 In pandas.在大熊猫。 That is an issue, because you see the intention of pandas is that you utilized it is methods.这是一个问题,因为你看到 pandas 的意图是你使用它是方法。

To get a similar result I would recommend using the string acessor in your series.为了获得类似的结果,我建议在您的系列中使用字符串处理器。

So just do the following所以只需执行以下操作

df.Industries.str.split(',').tolist()

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

相关问题 如何迭代多列数据框中的每个列值? - How to iterate over each individual column values in multiple column dataframe? 如何从字符串中删除一个短语而不是单个单词 - How can I remove a phrase from a string but not the individual words __str__ 返回一个巨大的字符串而不是单个值。 Python 的新手 - __str__ returning one giant string instead of individual values. New to Python Python和Pandas:如何从单个列中缩短字符串值? - Python and Pandas: How do I shorten string values from an individual column? Python-如何将字符串与起始值为0的单词词典进行比较,然后通过出现它们增加其值? - Python - How can I compare a string to a dictionary of words with starting values of 0, and then increase their values by their occurrence? 如何将包含多个值(它是一个元组)的 label 提供给 keras model - How can I feed a label which consists in multiple values (it is a tuple) to a keras model 如何将包含单个值的单个字符串分开 - How do I separate a single string containing to individual values 如何在具有多个值的列中选择部分字符串 - How to pick part of a string in a column with multiple values 用Python中的大字串剥离列值 - Stripping column values by large string of words in Python 如何将值拆分为列中的 int 和 string 并计算持续时间 - How can I split values as int and string in column and calculate duration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM