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

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
K Patel
  • 15
  • 1
  • 4
  • 1
    take a look [Set value for particular cell in pandas DataFrame using index](https://stackoverflow.com/questions/13842088/set-value-for-particular-cell-in-pandas-dataframe-using-index) – Bernardo stearns reisen Apr 15 '20 at 13:41

3 Answers3

2

Try this:

df['Name'] = df['Name'].str.replace('Agriculture, forestry and fishing', 'Agriculture')
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
0

This should work for any data type:

df.loc[df.loc[:, 'Name']=='Agriculture, forestry and fishing', 'Name'] = 'Agriculture'
Ehsan
  • 12,072
  • 2
  • 20
  • 33
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()