1

I have a fairly large Dataframes 22000X29 . I want to clean up one particular column for data aggregation. A number of cells can be replaced by one column value. I would like to write a function to accomplish this using replace function. How do I pass the column name to the function?

I tried passing the column name as a variable to the function. Of course, I could do this variable by variable, but that would be tedious

#replace in df from list
def replaceCell(mylist,myval,mycol,mydf):
    for i in range(len(mylist)):
        mydf.mycol.replace(to_replace=mylist[i],value=myval,inplace=True)
    return mydf

replaceCell((c1,c2,c3,c4,c5,c6,c7),c0,'SCity',cimsBid)

cimsBid is the Dataframes, SCity is the column in which I want values to be changed

Error message:

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

4 Answers 4

1

Try accessing your column as:

mydf[mycol]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did it for me!
I saw your answer immediately after posting, system said to wait for a minute. Got back one now
0

On this command:

mydf.mycol.replace(to_replace=mylist[i],value=myval,inplace=True)

Pandas columns access by attribute operator doesn't allows on variable name. You need to access it through indexing operator [] as:

mydf[mycol].replace(to_replace=mylist[i],value=myval,inplace=True)

There are few more warnings here

Warning

  • You can use this access only if the index element is a valid Python identifier, e.g. s.1 is not allowed. See here for an explanation of valid identifiers.
  • The attribute will not be available if it conflicts with an existing method name, e.g. s.min is not allowed.

  • Similarly, the attribute will not be available if it conflicts with any of the following list: index, major_axis, minor_axis, items.

  • In any of these cases, standard indexing will still work, e.g. s['1'], s['min'], and s['index'] will access the corresponding
    element or column.

Comments

0

hi try these function hopefully it will work

def replace_values(replace_dict,mycol,mydf):                                                                                       
   mydf = mydf.replace({mycol: replace_dict})
   return mydf

pass replacing values as dictonary

Comments

0

Address the column as a string. You should pass the whole list of values you want to replace (to_replace) and a list of new values (value). (Don't use tuples. If you want to replace all values with the same new value, it might be best

def replaceCell(mylist,myval,mycol,mydf):
    mydf[mycol].replace(to_replace=mylist,value=myval,inplace=True)
    return mydf
# example dataframe
df = pd.DataFrame( {'SCity':['A','D','D', 'B','C','A','B','D'] ,
                    'value':[23, 42,76,34,87,1,52,94]})
# replace the 'SCity' column with a new value
mylist = list(df['SCity'])
myval = ['c0']*len(mylist)
df = replaceCell(mylist,myval,'SCity',df)

# the output
df
    SCity   value
0   c0      23
1   c0      42
2   c0      76
3   c0      34
4   c0      87
5   c0       1
6   c0      52
7   c0      94

This returns the df, with the replaced values.

If you intend to only change a few values, you can do this in a loop.

def replaceCell2(mylist,myval,mycol,mydf):
    for i in range(len(mylist)):
        mydf[mycol].replace(to_replace=mylist[i],value=myval,inplace=True)
    return mydf
# example dataframe
df = pd.DataFrame( {'SCity':['A','D','D', 'B','C','A','B','D'] ,
                    'value':[23, 42,76,34,87,1,52,94]})

# Only entries with value 'A' or 'B' will be replaced by 'c0'
mylist = ['A','B']
myval = 'c0'
df = replaceCell2(mylist,myval,'SCity',df)

# the output
df
    SCity   value
0   c0      23
1   D       42
2   D       76
3   c0      34
4   C       87
5   c0       1
6   c0      52
7   D       94

2 Comments

I see that you are replacing the entire list in one shot instead of going item by item. Guess the time saved would be critical in larger Dataframes? Thanks for the insight.
The loop is necessary if only a few of the values should be replaced

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.