1
import pandas as pd

names = ['Bob','Jessica','Mary','John','Mel']
births = [968,155,77,578,973]

BabyDataSet = list(zip(names,births))
df = pd.DataFrame(data = BabyDataSet, columns=['Names','Births'])

df.at[3,'Names'].str.replace(df.at[3,'Names'],'!!!')

I want to change 'John' to '!!!' without directly referring 'John'.

In this way, it notice me "AttributeError: 'str' object has no attribute 'str'"

2 Answers 2

1
import pandas as pd

names = ['Bob','Jessica','Mary','John','Mel']
births = [968,155,77,578,973]

BabyDataSet = list(zip(names,births))
df = pd.DataFrame(data = BabyDataSet, columns=['Names','Births'])

df.loc[3,'Names'] = '!!!'
print(df)

Output:

     Names  Births
0      Bob     968
1  Jessica     155
2     Mary      77
3      !!!     578
4      Mel     973
Sign up to request clarification or add additional context in comments.

Comments

0

You should replace with series not single value ,single value also called assign

df['Names'] = df['Names'].str.replace(df.at[3,'Names'],'!!!')
df
Out[329]: 
     Names  Births
0      Bob     968
1  Jessica     155
2     Mary      77
3      !!!     578
4      Mel     973

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.