1

Here I attached my data frame.I am trying to change specific value of row.but I am not getting succeed.Any leads would be appreciated.

df.replace(to_replace ="Agriculture, forestry and fishing   ", 
             value ="Agriculture") 

Image of My data frame

1

3 Answers 3

2

Try this:

df['Name'] = df['Name'].str.replace('Agriculture, forestry and fishing', 'Agriculture')
Sign up to request clarification or add additional context in comments.

1 Comment

@Mayank Porwel thanks a ton! for providing easy solution
0

This should work for any data type:

df.loc[df.loc[:, 'Name']=='Agriculture, forestry and fishing', 'Name'] = 'Agriculture'

Comments

0

You can easily get all the columns names with calling: df.columns then you can copy this list and replace the name of any column and reassign the list to df.columns. For example:

    import pandas as pd 
    df = pd.DataFrame(data=[[1, 2], [10, 20], [100, 200]], columns=['A', 'B'])
    df.columns

the output will be in a jupyter notebook: Index(['C', 'D'], dtype='object')

so you copy that list and then replace what you want to change and reassign it

    df.columns = ['C', 'D']

and then you will get a dataframe with the name of columns changed from A and B to C and D, you check this by calling

    df.head()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.