2016-11-09 48 views
1

字符串轉換我有類似「1.0秒」,「100毫秒」等我不能繪製該值的numpy的系列(有熊貓,把陣列分成一系列後),因爲熊貓不承認這些是數字。如何讓numpy或熊貓將這些數據推算爲數字,同時注意後綴?隨着時間的推移後綴數字在numpy的

+2

右出的文檔的:http://pandas.pydata.org/pandas-docs/stable/timedeltas.html – Jeff

回答

0

此代碼可以解決你的問題。

# Test data 
se = Series(['10s', '100ms', '1.0s']) 

# Pattern to match ms and as integer of float 
pat = "([0-9]*\.?[0-9]+)(ms|s)" 
# Extracting the data 
df = se.str.extract(pat, flags=0, expand=True) 
# Renaming columns 
df.columns = ['value', 'unit'] 
# Converting to number 
df['value'] = pd.to_numeric(df['value']) 
# Converting to the same unit 
df.loc[df['unit']=='s', ['value', 'unit']] = (df['value'] * 1000, 'ms') 

# Now you are ready to plot ! 
print(df['value']) 
# 0  10000.0 
# 1  100.0 
# 2 100000.0 
+0

正則表達式?現在他有兩個問題......;) –

相關問題