Home > Software engineering >  Python Pandas - Can you use .loc and ignore indexes?
Python Pandas - Can you use .loc and ignore indexes?

Time:11-30

I am trying to replace a string found in a column with

file1_backup_df.loc[file1_backup_df['CustName'].str.contains('bbb', case=False), 'CustomerName'] = 'Big Boy Booty'

Now the above works on a single dataframe (file1_backup_df). But I am combining dataframes like this;

frames = [add_backup_name(), file1_backup_df]
final_df = pd.concat(frames)

I'd like to perform the very first line of code on final_df. But I can't.

It grumbles about

__setitem__
indexer = self._get_setitem_indexer(key)`.

ValueError: Cannot mask with non-boolean array containing NA / NaN value

Is there a way to replace strings in a column of my combined df?

I tried this but no go;

pd.concat(frames, ignore_index=True)

EDIT

Looks like this may have done it. Testing.

https://www.statology.org/cannot-mask-with-non-boolean-array-containing-na-nan-values/#:~:text=2022 by Zach-,How to Fix: ValueError: Cannot mask with non-boolean,array containing NA / NaN values&text=This error usually occurs when,searching in has NaN values.

CodePudding user response:

It seems that the column CustomerName holds some NaN values, so you need to set na=False in pandas.Series.str.contains :

Try this :

final_df.loc[final_df['CustName'].str.contains('bbb', case=False, na=False), 'CustomerName'] = 'Big Boy Booty'
  • Related