mercredi 30 septembre 2015

"If" statement dependent on time range

Say we have a data set similar to:

DF = pd.DataFrame({'Time':[1,2,3,4,5,6,7,8,9,10],'Value': [1,3,5,5,6,8,9,5,6,7]})

giving:

   Time  Value
0     1      1
1     2      3
2     3      5
3     4      5
4     5      6
5     6      8
6     7      9
7     8      4
8     9      6
9    10      7

What I want to do is set all values that are > 5 to equal 0 in "Values" but only when "Time" is > 5. End product would be:

   Time  Value
0     1      1
1     2      3
2     3      5
3     4      5
4     5      6
5     6      0
6     7      0
7     8      4
8     9      0
9    10      0

I have been using a code:

 DF.Value = [0 if x > 5 else x for x in DF.Value] 

Which obviously changes all values to 0 if they are > 5. I have tried adding things to this code such as:

  DF.Value = [0 if x > 5 in DF.value and x < 4 in DF.Time else x for x in DF.Value]

But I can't seem to get the right combination of words/code to yield what I want. Any suggestions? Thank you.

Aucun commentaire:

Enregistrer un commentaire