python - 我试图弄清楚如何从我的 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
解决方案
利用:
phillies_pr.loc[:,7]= phillies_pr.loc[:,7].str.replace('\$|,','').astype(int)
推荐阅读
- closedxml - 如何从 IXLCells (OpenXML) 中获取值
- c++ - 使用递归 C++ 的权力
- python - python如何将两个具有相同形状的4x1矩阵相乘
- vba - 在我的 PowerPoint 演示文稿的每张幻灯片的笔记上运行它
- python - 如果另一个系列包含特定字符串,如何在数据框中创建一个返回值的系列?
- apache-kafka - 卡夫卡经纪人领导者倾斜
- list - 根据 Haskell 中的索引交换列表列表中的两个元素
- kdb - KDB/Q:如何为 select by 编写函数
- reactjs - 临时反应组件中的样式化组件
- jmeter - 如何使用 Jmeter 以 120 个请求的 RPS 在 5 分钟内发送 36000 个请求?