Using pandas version 1.0.5
I have a following dataframe:
test = {'Price': ['Free','free', '-16.66', 'Name', '']}
df = pd.DataFrame(test)
df.loc[df['Price'].astype(str).str.contains(':'), ['col_1', 'col_2']] = df['Price'].astype(str).str.split(':',1,expand=True)
Here in this e.g. if value contains : then I need to split the values and need to assign two parts to two new cols col_1 and col_2 respectively.
But I get this error:
KeyError: "None of [Index(['col_1', 'col_2'], dtype='object')] are in the [columns]"
What am I missing here?
EDIT: I tried without .loc
df[df['Price'].astype(str).str.contains(':'), ['col_1', 'col_2']] = df['Price'].astype(str).str.split(':',1,expand=True)
And got this error:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
1.0.5