How to extract values from pandas Series without index
Asked Answered
H

4

23

I have the following when I print my data structure:

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

I believe this is a pandas Series but not sure

I do not want the first column at all I just want

-24.4
-12.2
-12.2
-12.2
-12.2

I tried 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

How can I just get the Speed values with index starting at 0?

Hartfield answered 29/5, 2020 at 13:4 Comment(2)
From your last line, you can go one more step: speed_tf.reset_index()["Speed"]... though I bet there's a nicer way.Hyperboloid
"I believe this is a pandas Series but not sure" It's always nice to be sure. Try: print(type(speed_tf))Londalondon
K
31
speed_tf.values 

Should do what you want.

Kekkonen answered 29/5, 2020 at 13:7 Comment(3)
This solution provides a list, whereas the other solution provides a DataFrame.Charioteer
@CedricZoppolo First, this solution provides a numpy array, not a list, but secondly, I am not sure what to make of your comment. Do you view this as a good or a bad thing?Kekkonen
You are right. values outputs an array type. I was just pointing out the difference of outputs between these two solutions. I believe none of them is better than the other. It depends on further implementation you may need.Charioteer
F
6

Simply use Series.reset_index and Series.to_frame:

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

Result:

# print(df)

   Speed
0  -24.4
1  -12.2
2  -12.2
3  -12.2
4  -12.2
Farmergeneral answered 29/5, 2020 at 13:14 Comment(0)
C
5

To print only the Speed values, with no indices:

print(speed_tf.to_string(index=False))
Choric answered 1/10, 2022 at 12:22 Comment(0)
B
0

This is for anyone willing to create a dataframe having 2 columns: series indices and series values.

# firstColumnName you choose to give
df = pd.DataFrame({'firstColumnName': speed_tf.index, 'Speed': speed_tf.values})
Beak answered 15/11, 2023 at 17:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.