pandas AttributeError: 'str' 对象没有属性 'str'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54191821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'str' object has no attribute 'str'
提问by Data_is_Power
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".
我的pandas
DataFrame
样子如下。我正在尝试从我的列中删除'$'
和然后应用到我的原始数据框上。所以我创建了下面的函数。但是,它给了我错误说','
income
"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.
注意:我是 python 新手,所以请提供解释。
My Dataframe:
我的数据框:
df1=pd.DataFrame(
{'Name': ['a', 'b','c','d'],
'income': ['', ',000',',000','0,000']})
My Function:
我的功能:
def column_replace(x):
return x.str.replace('$', '').str.replace(',','').apply(lambda x: column_replace(x))
回答by lexual
In [23]: df1
Out[23]:
Name income
0 a
1 b ,000
2 c ,000
3 d 0,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