Dat*_*wer 1 python string pandas
我的pandas DataFrame样子如下。我正在尝试从我的列中删除'$'和然后应用到我的原始数据框上。所以我创建了下面的函数。但是,它给了我错误说','income"str" object has no attribute "str".
非常感谢有关如何解决此问题的任何建议。
注意:我是 python 新手,所以请提供解释。
我的数据框:
df1=pd.DataFrame(
{'Name': ['a', 'b','c','d'],
'income': ['$1', '$2,000','$10,000','$140,000']})
Run Code Online (Sandbox Code Playgroud)
我的功能:
def column_replace(x):
return x.str.replace('$', '').str.replace(',','').apply(lambda x: column_replace(x))
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26117 次 |
| 最近记录: |