Home > Net >  python, pandas, I have data like <16 * MonthEnds>, how to compare it to an integer?
python, pandas, I have data like <16 * MonthEnds>, how to compare it to an integer?

Time:05-12

my data looks like this:

<16 * MonthEnds>
<19 * MonthEnds>
<23 * MonthEnds>
<12 * MonthEnds>
<24 * MonthEnds>
<13 * MonthEnds>

and i want to do something like:

if Duration<12:
    do something

but i can't find away to compare the duration with an integer

i always get an error

TypeError: '<' not supported between instances of 'pandas._libs.tslibs.offsets.MonthEnd' and 'int'

CodePudding user response:

I assume that your data looks something like this. Please let me know if it doesn't because MonthEnds might be a variable too.

data = ["<16 * MonthEnds>",
"<19 * MonthEnds>",
"<23 * MonthEnds>",
"<12 * MonthEnds>",
"<24 * MonthEnds>",
"<13 * MonthEnds>"]

Then you can try this to get all the rows with values greater than 12.

data = pd.Series(data)
nums = pd.Series(map(lambda item:int(item[0].strip().lstrip("<")), data.str.split("*")))
valid_data = nums[nums > 12]
0    16
1    19
2    23
4    24
5    13
dtype: int64

CodePudding user response:

You can convert MonthEnds offset to integer:

df['Duration'] = df['Duration'].astype(str).str.extract('(\d )').astype(int)

# OR

df['Duration'] = df['Duration'].map(lambda x: getattr(x, 'n')) < 12

Output:

>>> df
   Duration
0        16
1        19
2        23
3        12
4        24
5        13
  • Related