字符串轉換我有類似「1.0秒」,「100毫秒」等我不能繪製該值的numpy的系列(有熊貓,把陣列分成一系列後),因爲熊貓不承認這些是數字。如何讓numpy或熊貓將這些數據推算爲數字,同時注意後綴?隨着時間的推移後綴數字在numpy的
1
A
回答
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
正則表達式?現在他有兩個問題......;) –
1
見問題how do I get at the pandas.offsets object given an offset string
- 使用
pandas.tseries.frequencies.to_offset
- 皈依timedeltas
- 獲得總秒數
from pandas.tseries.frequencies import to_offset
s = pd.Series(['1.0s', '100ms', '10s', '0.5T'])
pd.to_timedelta(s.apply(to_offset)).dt.total_seconds()
0 0.0
1 0.1
2 10.0
3 300.0
dtype: float64
右出的文檔的:http://pandas.pydata.org/pandas-docs/stable/timedeltas.html – Jeff