I have the following output from a txt file. My goal is to find the difference between values of Column 2 and Column 3 as long as the value in Column 1 remains below or equal to 5, that means my expected output is the difference of Column 2 and 3 values up to Row 5 as the Column 1 value in Row 6 is greater than 5.
I tried the following approach.
import pandas as pd
data= pd.read_table('/Users/Hrihaan/Desktop/A.txt', dtype=float, header=None, sep='\s+').values
x=data[:,0]
y=(data[:,1] for x<=5)
z=(data[:,2] for x<=5)
Diff=y-z
print(Diff)
I received this error: (SyntaxError: invalid syntax), any help on how to get it going would be really helpful.
import numpy as np
>>> np.where(data[:, 0] <= 5, data[:, 1] - data[:, 2], np.nan)
array([ -1., -1., 0., -3., 1., nan])
For your code, you can use a conditional list comprehension:
y = [i for x, i in zip(data[:, 0], data[:, 1]) if x <= 5]
z = [i for x, i in zip(data[:, 0], data[:, 2]) if x <= 5]
diff = [a - b for a, b in zip(y, z)]
or...
diff = [y - z for x, y, z in data if x <= 5]
Or you can try this
(df2['v2'].subtract(df2['v3']))[(df2['v1']<=5)]
Out[856]:
0 -1
1 -1
2 0
3 -3
4 1
dtype: int64
Data input
df2
Out[857]:
v1 v2 v3
0 1 4 5
1 2 6 7
2 3 8 8
3 4 4 7
4 5 3 2
5 6 8 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With