Attributeerror: 'Str' Object Has No Attribute 'Str'

Attributeerror: 'Str' Object Has No Attribute 'Str'

My pandas DataFrame looks like following. I am trying to remove '$' and ',' from my income column and then apply on my original dataframe. so I created below function. However, it is giving me error Saying "str" object has no attribute "str".

Any suggestion on how to fix this is greatly appreciated.

Note: I am new to python so please provide explanation.

My Dataframe:

df1=pd.DataFrame(
     {'Name': ['a', 'b','c','d'],
     'income': ['$1', '$2,000','$10,000','$140,000']})

My Function:

def column_replace(x):
    return x.str.replace('$', '').str.replace(',','').apply(lambda x: column_replace(x))
7

1 Answer

In [23]: df1
Out[23]: 
  Name    income
0    a        $1
1    b    $2,000
2    c   $10,000
3    d  $140,000

In [24]: cols_to_change = ['income']

In [25]: for col in cols_to_change:
    ...:     df1[col] = df1[col].str.replace('[$,]', '')
    ...: 

In [26]: df1
Out[26]: 
  Name  income
0    a       1
1    b    2000
2    c   10000
3    d  140000
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.