0
$\begingroup$

Assume I have a dataset with three inputs:

   x1   x2  x3  
0  a    b   c
1  d    e   f
2  g    h   i
3  j    k   l
4  m    n   o
5  p    q   r
6  s    t   u 
      :
      :

0,1,2,3 are times, x1, x2, x3 are inputs that are measured. So here x1 inputs are measured at every one hour. x2 and x3 will be measured at different time. What I need to do , I want write that what ever the measured in x1, x2, x3 it will add and subtract the values are equal to the x1 input next time value So here what I want to do is:

    x1   x2   x3   y
 0  a    b    c   a+b-c==d
 1  d    e    f   d+e+f==g
 2  g    h    i   g+h-i==j
 3  j    k    l   j+k-l==m
 4  m    n    o   m+n-o==p
 5  p    q    r   p+q-r==s
 6  s    t    u   s+t-u==v
         :
         :

I tried shift method and it didn't work for me what I want exactly. I tried another method and it worked, but didn't came as I want. Here I upload the code.

Code :
 data = pd.read_csv('data6.csv')
 i=0
 j=1
 while j < len(data):
   j=data['x1'][i] - data['x2'][i] + data['x3'][i] 
   i+=1 
   j!=i 
  print(j)

This is works , but it is just showing only one data

63

In my csv file this is second input value of x1 input. I want to write this code contonously happened and read the value as I shown above. Can anyone help me to solve this problem?

My csv file

$\endgroup$
2
$\begingroup$

So are you evaluating the equality of a+b-c and d for each sample? Meaning, you aim to save the boolean value? If so:

import pandas as pd
#a sample dictionary
data = {'x1':[1,0,4,5,8,1], 
     'x2':[3,4,5,6,8,9],
     'x3':[4,5,1,-2,4,5]}
df = pd.DataFrame(data) #converting the dictionary to dataframe
df['y'] = 0 #initializing the y column with 0, also you can initialize with FALSE boolean value
for i in range(len(df)-1): #iterating between all the rows of dataframe
    df['y'].iloc[i] = df['x1'].iloc[i] + df['x2'].iloc[i] - df['x3'].iloc[i] == df['x1'].iloc[i+1] 

Note: for the last row, since the content of column y should be calculated based on the next row, the value cannot be calculated, that is why we have set (len(df)-1).

Result:

   x1  x2  x3      y
0   1   3   4   True
1   0   4   5  False
2   4   5   1  False
3   5   6  -2  False
4   8   8   4  False
5   1   9   5      0

The problem with your code: First, you are not considering that subsequent row to calculate the accuracy of the expression $a+b-c ==d$ Second, if that print(j) is outside the loop, is it? then you are looping in the rows of data frame while j is less than data frame's length, So when the flow is out of this while, a single value j would be returned.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for the fast reply. I used your code , and it shows the same as you shown below. But I want to show the values not true or false, If I want to display value then how to chsnge the code? $\endgroup$ – bala Jul 14 '19 at 4:17
  • $\begingroup$ yes j is in out loop, what about write in while loop ? $\endgroup$ – bala Jul 14 '19 at 4:22
  • $\begingroup$ Here what I want is by adding or subtracting what ever the value is coming it will be equal to the x1 column next value (!=) $\endgroup$ – bala Jul 14 '19 at 4:24
  • 1
    $\begingroup$ If so you just need to write: python df['y'].iloc[i] = df['x1'].iloc[i+1] in the last line of the code. $\endgroup$ – Fatemeh Asgarinejad Jul 15 '19 at 3:09
  • 1
    $\begingroup$ Wooww it worked . Thank you. $\endgroup$ – bala Jul 15 '19 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.