2014-03-24 27 views
2

比方說,我有下面的例子中數據幀的Python,熊貓:如何刪除大於號

from pandas import Series, DataFrame 
df = DataFrame({'A':['1', '<2', '3']}) 

我想在A列從字符串到整數轉換。在'< 2'的情況下,我想簡單地取下'<'符號,並將第二行中的1(最接近的整數小於2)。什麼是最有效的方法呢?這只是一個例子。我正在處理的實際數據有數十萬行。 感謝您的幫助提前。

回答

3

你可以使用Series.apply

import pandas as pd 
df = pd.DataFrame({'A':['1', '<2', '3']}) 
df['A'] = df['A'].apply(lambda x: int(x[1:])-1 if x.startswith('<') else int(x)) 
print(df.dtypes) 
# A int64 
# dtype: object 

產量

print(df) 
    A 
0 1 
1 1 
2 3 

[3 rows x 1 columns] 
1

您可以在數據框使用applymap,如果它出現在字符串中刪除 「<」 字:

df.applymap(lambda x: x.replace('<','')) 

這裏是輸出:

A 
0 1 
1 2 
2 3 
0
>>> import re 
>>> df.applymap(lambda x: int(re.sub(r'[^0-9.]', '', x))) 
    A 
0 1 
1 2 
2 3 
1

這裏有可能是在旅途中,前有幫助做這兩種其他的方式!

from pandas import Series, DataFrame 
df = DataFrame({'A':['1', '<2', '3']}) 

輸出

df.A.str.strip('<').astype(int) 
Out[1]: 
0 1 
1 2 
2 3 

如果你試圖在你的電話號碼的中間刪除一個字符(例如,如果你有一個逗號或某事),這種方式將是有益的。

df = DataFrame({'A':['1', '1,002', '3']}) 
df.A.str.replace(',', '').astype(int) 

輸出

Out[11]: 
0  1 
1 1002 
2  3 
Name: A, dtype: int64