1

Is there any way to access the first element of the python series object that may contain a list in some entries

Example input

JUNE      0.1591
JULY      0.004
AUG       0.000
SEPT      NaN
OCT   [1.004, 0.000]
dtype: object

Desired output

JUNE      0.1591
JULY      0.004
AUG       0.000
SEPT      NaN
OCT   1.004
dtype: object
yatu
  • 86,083
  • 12
  • 84
  • 139
shams
  • 152
  • 11
  • Can you provide some more context for this? Why is your data a mix of numbers and lists? – AMC Feb 03 '20 at 23:54
  • actual context is `[NaN, NaN]`, that was getting from a loop like ```testdata[month]=ttest_ind(x_sample[month], y_sample[month], equal_var=False)[0]```. – shams Feb 04 '20 at 03:44
  • 1
    Difficult to know what’s going on based only on that. I was asking because I suspect there is a better way of solving this, or perhaps a way to avoid the issue entirely. – AMC Feb 04 '20 at 03:50
  • agree. I was working with https://github.com/thomasmaxwellnorman/Perturbseq_GI/blob/master/GI_activation_of_neighboring_genes.ipynb the `bootstrap_ratio` function breaks due to the list in the array produced by `bootstrap_t ` in my data. So was thinking to work around to take first element from array. Hope this clarify. Was avoiding this context to make it simple and could'nt think appropriate data structure being a python beginner – shams Feb 04 '20 at 03:59
  • the exact error is ```ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ``` – shams Feb 04 '20 at 04:09
  • Regarding that error, see https://stackoverflow.com/q/10062954/11301900. – AMC Feb 04 '20 at 04:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207145/discussion-between-shams-and-amc). – shams Feb 04 '20 at 04:47

1 Answers1

1

Here's one way using Series.mask:

s.mask(s.str.len().gt(1), s.str[0])

JUNE    0.1591
JULY     0.004
AUG          0
SEPT       NaN
OCT      1.004
yatu
  • 86,083
  • 12
  • 84
  • 139