[英]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.