我正在尝试找出如何从csv文件中删除逗号和美元符号

时间:2019-06-25 10:01:50

标签: python pandas csv

我目前正在处理涉及工资的csv文件。我想去除美元符号和逗号,以便将字符串转换为整数。

import csv
import numpy as np
import random
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt

phillies_of = pd.read_csv('/Users/hannahbeegle/Desktop/Teams/PHILLIES.csv', header = None)
phillies_pr = pd.read_csv('/Users/hannahbeegle/Desktop/Teams/PHILLIES_PR.csv',header = None)

phillies_pr.loc[:,7]= phillies_pr.loc[:,7].replace('$','')
phillies_pr.loc[:,7]= phillies_pr.loc[:,7].replace(',','')
filter = phillies_pr.loc[:,7]= ''
phillies_pr.where(filter).dropna()
fan_attendance = [int(i) for i in phillies_pr.iloc[:,7]]

ERROR MESSAGE:
Traceback (most recent call last):
  File "/Users/hannahbeegle/Desktop/Text Files/TeamDataBase.py", line 68, in <module>
    phillies_pr.where(filter).dropna()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 8834, in where
    errors=errors, try_cast=try_cast)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 8572, in _where
    raise ValueError('Array conditional must be same shape as '
ValueError: Array conditional must be same shape as self

1 个答案:

答案 0 :(得分:0)

使用:

phillies_pr.loc[:,7]= phillies_pr.loc[:,7].str.replace('\$|,','').astype(int)