0

I tried to append a new column to my dataframe. It doesn't work... well... it does work, but it always throws SettingWithCopyWarnings.

I used all the methods proposed here, and searched also other Stackoverflow threads for a solution, but still I don't arrive.

I have a dynamic list of words that shall be added to my DataFrame as new Columns. In each column there shall be the count of words as found in a line of text.

import re
import pandas as pd

df = pd.read_csv('manyLinesOfText.csv')

word_lst = ['foo', 'bar', 'spam']

def element_word_count(text, **kwds):
    return float(len(re.findall(kwds['kwds']['countword'], text)))

for word in word_lst:
    df[word] = df['textcolumn'].apply(element_word_count, kdws = {'countword':word})

This works, but throws an annoying warning.

I tried assign but it doesn't work at all, because the syntax of assign interprets the "words" as the title of the column.

I tried df.insert, but it throws a NoneType exception, which is weird because my df definitely is not NoneType.

I tried df.loc[:,word] but it does solve nothing. It works, and the same exception is thrown.

Maybe I have just a knot in my brain after the entire day worth of work, but I cant't find the solution.

I also think my use of df.apply is a bit strange - so if someone finds an easier way to count a given but dynamic list of words, I am open to anything.

Thanks a lot!

Anderas
  • 630
  • 9
  • 20

1 Answers1

0

I found the failure. Thanks to everybody who took the time to read through my posting. The code here is actually ok. The problem was found elsewhere, after the correction elsewhere, the warning disappeared in this part.

Anderas
  • 630
  • 9
  • 20