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...
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))
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