python - 尝试在 Python 中使用 Pandas 删除逗号和美元符号

标签 python pandas

删除列中的逗号和美元符号。但是当我这样做时,表格会将它们打印出来并且仍然在那里。是否有其他方法可以使用 pandas 函数删除命令和美元符号。我无法在 API Docs 中找到任何内容,或者我找错地方了

 import pandas as pd
    import pandas_datareader.data as web

players = pd.read_html('http://www.usatoday.com/sports/mlb/salaries/2013/player/p/')


df1 = pd.DataFrame(players[0])


df1.drop(df1.columns[[0,3,4, 5, 6]], axis=1, inplace=True)
df1.columns = ['Player', 'Team', 'Avg_Annual']
df1['Avg_Annual'] = df1['Avg_Annual'].replace(',', '')

print (df1.head(10))

最佳答案

您必须根据 http://pandas.pydata.org/pandas-docs/stable/text.html 访问 str 属性

df1['Avg_Annual'] = df1['Avg_Annual'].str.replace(',', '')
df1['Avg_Annual'] = df1['Avg_Annual'].str.replace('$', '')
df1['Avg_Annual'] = df1['Avg_Annual'].astype(int)

交替;

df1['Avg_Annual'] = df1['Avg_Annual'].str.replace(',', '').str.replace('$', '').astype(int)

如果您想优先考虑打字时间而不是可读性。

关于python - 尝试在 Python 中使用 Pandas 删除逗号和美元符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516481/

相关文章:

Python:消除数据框中重复的小数

python - Libtorrent 在下载开始前设置上传/下载限制

python - 在 Python 中查找和比较文件

python - 从多个 DataFrame Python 中收集重复的列名称

python - 从数据帧的不同部分减去两个系列

python - 安全地将 float 向下转换为尽可能小的整数类型

python - 将字符串转换为列表 Python

python - 在 Pandas 数据框 python 中寻找值(value)

python - 将多行合并到数据框列的一行

python - 使用每月日期计算累积流失率 - 日期问题很重要