0

I know there are a lot of questions (and answers) about this, but I cannot solve this particular cases using the recommended solutions:

1)

df=df.dropna(how='any')

I changed it to

df.loc[:,:]=df.dropna(how='any')

But if I do it, the dropna does not work.

Should I use for this case

df.dropna(how='any', inplace=True)

to avoid the reassignment?


2) & 3)

column_name='my_name'

df[column_name]=df.apply(lambda row: nltk.word_tokenize(row[column_names]), axis=1)
df[column_name]=df.apply(lambda row: [w for w in row[column_name]if not w in stop_words],axis=1)

I changed the two df[column_name] of the left side by

df.loc[:,column_name]

But I get an error with the axis parameter

Which is the best solution for these cases?

Laura
  • 1,192
  • 2
  • 18
  • 36
  • 1
    This usually happens if your code has `df = some_df.loc[...]` before those lines. – Quang Hoang Dec 10 '19 at 17:05
  • [Related](https://stackoverflow.com/questions/33727667/pandas-settingwithcopywarning-a-value-is-trying-to-be-set-on-a-copy-of-a-slice). Whenever you are manipulating a dataframe and then doing an operation which can potentially change the shape of the dataframe this can occur. The best solution would be to create a copy of the dataframe first and then manipulate. However sometimes I bite the bullet if the dataframe is large and accept the warning. – Edeki Okoh Dec 10 '19 at 17:19
  • [Also Related](https://stackoverflow.com/questions/27673231/why-should-i-make-a-copy-of-a-data-frame-in-pandas) – Edeki Okoh Dec 10 '19 at 17:19

0 Answers0