在我的脚本中,我想读取一些csv文件并同时转换输入值。但是一列的值取决于另一列(此列将不被转换)的值。有什么方法可以在read_csv中实现,还是在读取csv之后必须更改它?
file.csv
date total percentage
03/25/2017 100 1%
04/15/2016 200 6%
预期产量
date total success
03/25/2017 100 1
04/15/2016 200 12
def convert_succes(percentage):
# is there any way to pass an 'total' value to this function?
return percentage / 100
names = ['date', 'total', 'success']
converters = {
'date': pandas.to_datetime,
'success': convert_succes,
}
input_report = pandas.read_csv('file.csv', names=names, converters=converters)
答案 0 :(得分:1)
将字符串%
剥离并转换为float然后相乘:
df['success']=df.total*df.percentage.str.rstrip('%').astype('float') / 100.0
print(df)
date total percentage success
0 03/25/2017 100 1% 1.0
1 04/15/2016 200 6% 12.0
要在读取文件时将其从字符串转换为浮点,请使用here中的以下内容:
def p2f(x):
return float(x.strip('%'))/100
pd.read_csv(file, sep='whatever',converters={'percentage':p2f})