[英]How can I convert a Pandas Series to a Numpy Array and maintain order?
I have a pandas
series that looks like: 我有一个
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
I want to convert it to a numpy
array, so I'm doing: 我想将其转换为一个
numpy
数组,所以我在做:
next_state_val = np.array([next_state.values])
However, there's no guarantee that my series
will always have the same order. 但是,不能保证我的
series
将始终具有相同的顺序。 How can I maintain the same order across many series? 如何在多个系列中保持相同的顺序?
Assuming the indices are always the same (but not necessarily occurring in the same order), you can use .sort_index()
on the series. 假设索引始终相同(但不一定以相同的顺序出现),则可以在系列中使用
.sort_index()
。 This will ensure the series is consistently ordered by its index each time. 这将确保该系列每次都按其索引一致地排序。
https://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.sort_index.html 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.