[英]How can I convert a Pandas Series to a Numpy Array and maintain order?
我有一個pandas
系列,看起來像:
cash 50121.599128
num_shares 436.000000
cost_basis 114.400002
open_price 113.650002
close_10 114.360001
close_9 115.769997
close_8 114.800003
close_7 114.040001
close_6 115.680000
close_5 115.930000
close_4 115.430000
close_3 113.339996
close_2 114.870003
close_1 114.050003
dtype: float64
我想將其轉換為一個numpy
數組,所以我在做:
next_state_val = np.array([next_state.values])
但是,不能保證我的series
將始終具有相同的順序。 如何在多個系列中保持相同的順序?
假設索引始終相同(但不一定以相同的順序出現),則可以在系列中使用.sort_index()
。 這將確保該系列每次都按其索引一致地排序。
https://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.sort_index.html
如果我正確理解了文檔,則.values
方法將返回一個數組並保留順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.