简体   繁体   中英

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). I imported the dataframe and converted the column I'm seeking to write a for loop into a list. 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.

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

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