Borut Flis
Borut Flis

Reputation: 16375

Replace string in one part pandas dataframe

print(df["date"].str.replace("2016","16"))

The code above works fine. What I really want to do is to make this replacement in just a small part of the data-frame. Something like:

df.loc[2:4,["date"]].str.replace("2016","16")

However here I get an error:

AttributeError: 'DataFrame' object has no attribute 'str'

Upvotes: 0

Views: 48

Answers (1)

gosuto
gosuto

Reputation: 5741

What about df['date'].loc[2:4].str.replace('2016', 16')?

By selecting ['date'] first you know you are dealing with a series which does have a string attribute.

Upvotes: 2

Related Questions