Question

[Solved] 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('


Solution #1:
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
Respondent: lexual

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

, '').str.replace(',','').apply(lambda x: column_replace(x))

Solution #1:

Respondent: lexual
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy