Home > OS >  Extracting certain values from the Pandas Series with float64 datatype
Extracting certain values from the Pandas Series with float64 datatype

Time:08-31

I have a Pandas series with float64 datatype, and I would like to extract certain values from that series.

Example, x=pd.Series([1.0, 1.4, 1.8, 2.0, 2.6, 3.0],copy=False)

I would like to extract only "1.0, 2.0 and 3.0" from the above series, x. How can I achieve it?

CodePudding user response:

If you know the indices of your numbers, you can do this:

x = pd.Series([1.0, 1.4, 1.8, 2.0, 2.6, 3.0],copy=False)
x.iloc[[0, 3, 5]].tolist()
>>> [1.0, 2.0, 3.0]

If you want to extract only those floats that are whole numbers:

x = pd.Series([1.0, 1.4, 1.8, 2.0, 2.6, 3.0],copy=False)
[i for i in x if i.is_integer()]
>>> [1.0, 2.0, 3.0]

Since some information is missing in your question, these solutions might not meet your requirements. If that is the case, please clarify your question.

  • Related