1

I have series like so:

d = {'a': 0, 'b': 1, 'c': 22, 'd': 537, 'e': 537, 'f': 0}
s = pd.Series(d)

I'd like to assign conditions so that:

NB: len(df) = 537

if x=0: "E"
if x=537: "F"
if 0<x<537: "P"

I have the following:

    df_len = len(df.index)
    no_of_na_empty = df.isna().sum()
    rslt_df = no_of_na_empty.loc[(no_of_na_empty > 0) & (no_of_na_empty < df_len)]
    no_of_na_empty = no_of_na_empty[rslt_df]
    no_of_na_empty = no_of_na_empty.mask(rslt_df, other="P")
    no_of_na_empty = no_of_na_empty.replace(to_replace=0, value="F")
    no_of_na_empty = no_of_na_empty.replace(to_replace=df_len, value="E")

However this doesn't work (for the condition if 0<x<537: "P"). The output should be:

d = {'a': "E", 'b': "P", 'c': "P", 'd': "F", 'e': "F", 'f': "E"}
s = pd.Series(d)```
Zoro
  • 420
  • 5
  • 16
pymat
  • 1,090
  • 1
  • 23
  • 45

1 Answers1

1

You can use np.select

import numpy as np

s = np.select([s==0, s==537],["E","F"], "P")

print(s)

Note: I'm assuming your series doesn't have values > 537

Sociopath
  • 13,068
  • 19
  • 47
  • 75