2014-09-04 73 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],我更喜歡明確刪除「%」,而不是盲目地去掉最後一個字符,以防萬一......

編碼愉快!