How come when i want to replace a value I have to use this block of code:
data['Organization'].str.replace('Greece','Rome')
why cant I use this:
data['Organization'].replace('Greece','Rome').
I've seen others use method two before without passing a string method. My question is can i pass a series method using replace function and what is the line of code?
pd.Series.replace
is different to pd.Series.str.replace
:
pd.Series.replace
is used to replace an element in its entirety. It will work also on non-string elements.pd.Series.str.replace
is used to replace substrings, optionally using regex.Here's a minimal example demonstrating the difference:
df = pd.DataFrame({'A': ['foo', 'fuz', np.nan]})
df['B'] = df['A'].replace(['foo', 'fuz'], ['food', 'fuzzy'])
df['C'] = df['A'].str.replace('f.', 'ba', regex=True)
print(df)
A B C
0 foo food bao
1 fuz fuzzy baz
2 NaN NaN NaN
str.replace
by default does a regex based replacement which also works with partial matches. replace
, OTOH, will only perform replacements based on full matches by default unless the regex
flag is set to true.
data['Organization'] = (
data['Organization'].replace({'Greece': 'Rome'}, regex=True))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With