如何从pandas Series中提取值而不包括索引?

23

当我打印我的数据结构时,我得到了以下结果:

print(speed_tf)

44.0   -24.4
45.0   -12.2
46.0   -12.2
47.0   -12.2
48.0   -12.2
Name: Speed, dtype: float64

我认为这是一个pandas Series,但不确定

我不想要第一列,我只需要

-24.4
-12.2
-12.2
-12.2
-12.2

我尝试了speed_tf.reset_index()

  index  Speed
0 44.0   -24.4
1 45.0   -12.2
2 46.0   -12.2
3 47.0   -12.2
4 48.0   -12.2

我应该如何仅获取索引从0开始的Speed值?


1
从你的最后一行开始,你可以再走一步:speed_tf.reset_index()["Speed"]...虽然我敢打赌还有更好的方法。 - Mateen Ulhaq
我认为这是一个pandas Series,但不确定。确信无妨,可以尝试使用以下代码:print(type(speed_tf)) - Spike0xff
4个回答

30
speed_tf.values 

应该做你想要的事情。


这个解决方案提供了一个列表,而另一个解决方案提供了一个数据框。 - Cedric Zoppolo
@CedricZoppolo 首先,这个解决方案提供了一个numpy数组,而不是一个列表,但是,我不确定你的评论意味着什么。你认为这是好事还是坏事? - Igor Rivin
1
你说得对。values 输出的是 array 类型。我只是指出了这两种解决方案之间输出的差异。我认为它们之间没有更好的一种。这取决于你可能需要的进一步实现。 - Cedric Zoppolo

5

只需使用 Series.reset_indexSeries.to_frame

df = speed_tf.reset_index(drop=True).to_frame()

结果:

# print(df)

   Speed
0  -24.4
1  -12.2
2  -12.2
3  -12.2
4  -12.2

4

只打印速度值,没有索引:

print(speed_tf.to_string(index=False))

0
这是给任何愿意创建一个包含两列的数据框的人:系列索引和系列值。
# firstColumnName you choose to give
df = pd.DataFrame({'firstColumnName': speed_tf.index, 'Speed': speed_tf.values})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接