0

I have a dataset that looks like this:

DayDate   Value
19-01-01  500
19-01-02  750
19-01-03  1000

And I want to create this data:

DayDate Value  IsAboveMedian
19-01-01  500    False
19-01-02  750    False
19-01-03  1000   True

How do I do that in Python, add a Boolean that is a calculation of another variable?

yatu
  • 86,083
  • 12
  • 84
  • 139
cherrypie
  • 23
  • 3
  • 1
    hint: `df['Value'].median` gives you the median, and then you can use that to compare. – Quang Hoang Sep 17 '19 at 15:15
  • 1
    Are you sure you want to do this? Anytime you add or delete a row, or update the `Value` of an existing row, the median will likely change. So now you have to go back and recalculate all the `IsAboveMedian` values. – John Gordon Sep 17 '19 at 15:17

2 Answers2

0

You simply need:

df['IsAboveMedian'] = df['Value']>df['Value'].median()

Output:

DayDate Value  IsAboveMedian
19-01-01  500    False
19-01-02  750    False
19-01-03  1000   True
harvpan
  • 8,571
  • 2
  • 18
  • 36
0

Hello this could help you. Check before posting question because this is already answered .

l = [2,58,8,69,9,7]
df  = pd.DataFrame(l, columns=['values'])
np.median(df['values'])

df["IsAboveMedian"] = df["values"].apply(lambda x: True if x>np.median(df['values']) else False)
abdoulsn
  • 842
  • 2
  • 16
  • 32