首页 > 解决方案 > 我试图弄清楚如何从我的 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

标签: pythonpandascsv

解决方案


利用:

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

推荐阅读