2014-09-04 75 views
11

当在熊猫中使用read_csv时,是否有方法将诸如'34%'之类的值直接转换为int或float?我希望它直接读为0.34。将百分比字符串转换为在熊猫中浮动read_csv

在read_csv使用这种没有工作:

read_csv(..., dtype={'col':np.float}) 

加载CSV为 'DF' 这也没有出现错误 “无效的文字浮法():34%” 下班后

df['col'] = df['col'].astype(float) 

最后我用这里面的作品,而是长篇大论:

df['col'] = df['col'].apply(lambda x: np.nan if x in ['-'] else x[:-1]).astype(float)/100 

谢谢,

回答

18

您可以自定义一个函数来转换您的百分比,以漂浮

In [149]: 
# dummy data 
temp1 = """index col 
113 34% 
122 50% 
123 32% 
301 12%""" 
# custom function taken from https://stackoverflow.com/questions/12432663/what-is-a-clean-way-to-convert-a-string-percent-to-a-float 
def p2f(x): 
    return float(x.strip('%'))/100 
# pass to convertes param as a dict 
df = pd.read_csv(io.StringIO(temp1), sep='\s+',index_col=[0], converters={'col':p2f}) 
df 
Out[149]: 
     col 
index  
113 0.34 
122 0.50 
123 0.32 
301 0.12 
In [150]: 
# check that dtypes really are floats 
df.dtypes 
Out[150]: 
col float64 
dtype: object 

我%的浮动代码是阿什维尼的回答礼貌:What is a clean way to convert a string percent to a float?

+1

非常有用,谢谢。我不知道'转换器'。 – KieranPC 2014-09-04 16:13:16

3

你是非常接近你的df尝试。尝试改变:

df['col'] = df['col'].astype(float) 

到:

df['col'] = df['col'].str.rstrip('%').astype('float')/100.0 
#     ^use str funcs to elim '%' ^divide by 100 
# could also be:  .str[:-1].astype(... 

大熊猫支持Python的字符串处理能力。只需在.str的字符串func之前,看看它是否满足您的需求。 (当然,这也包括字符串切片。)

上面我们利用.str.rstrip()来摆脱尾部百分号,然后我们将整个数组除以100.0以将百分比转换为实际值。例如,45%相当于0.45。

虽然.str.rstrip('%')也可能只是.str[:-1],我更喜欢明确删除“%”,而不是盲目地去掉最后一个字符,以防万一......

编码愉快!