2013-11-04 158 views
0

如何將單位列數字化?將一列字符串轉換爲熊貓中的數字

我有一個谷歌電子表格,我正在閱讀的日期列獲得轉換罰款..但我沒有很多運氣得到單位銷售列轉換爲數字我包括所有使用請求的代碼獲取數據:

from StringIO import StringIO 
import requests 
#act = requests.get('https://docs.google.com/spreadsheet/ccc?key=0Ak_wF7ZGeMmHdFZtQjI1a1hhUWR2UExCa2E4MFhiWWc&output=csv&gid=1') 
dataact = act.content 
actdf = pd.read_csv(StringIO(dataact),index_col=0,parse_dates=['date']) 
actdf.rename(columns={'Unit Sales': 'Units'}, inplace=True) #incase the space in the name is messing me up 

我試圖讓單位去數字

actdf=actdf['Units'].convert_objects(convert_numeric=True) 
#actdf=actdf['Units'].astype('float32') 

的不同方法。然後我想重新取樣和我越來越陌生的字符串串聯,因爲數量仍然字符串

#actdfq=actdf.resample('Q',sum) 
#actdfq.head() 
actdf.head() 
#actdf 

所以DF看起來是這個剛剛單位和日期索引

date 
2013-09-01 3,533 
2013-08-01 4,226 
2013-07-01 4,281 
Name: Units, Length: 161, dtype: object 

回答

3

您必須指定千位分隔符:

actdf = pd.read_csv(StringIO(dataact), index_col=0, parse_dates=['date'], thousands=',') 
+0

尼斯,人們會認爲,也許這應該是默認的... – dartdog

2
This will work 

In [13]: s 
Out[13]: 
0 4,223 
1 3,123 
dtype: object 

In [14]: s.str.replace(',','').convert_objects(convert_numeric=True) 
Out[14]: 
0 4223 
1 3123 
dtype: int64 
相關問題