How to compute row-wise comparison of multiple columns? - python
I got a table with lots of point informations and I need to fill the position field after row wise comparison of the four fields before.
If the X- & Y-Coordinate is equal and also the ID_01, a comparison of ID_02 is required to assign "End" into the Position field for the lower ID_02 value, hence the row with value 35 and "Start" into the one with row equal 36 as its larger.
X-Coordinate
Y-Coordinate
ID_01
ID_02
Position
45000
554000
15
35
?
45000
554000
15
36
?
94475
59530
1
1
94491
60948
1
1
94491
60948
1
2
94151
64480
1
2
94151
64480
1
3
95408
68694
1
3
95408
68694
1
4
94703
69961
1
4
94703
69961
1
5
93719
70786
1
5
93719
70786
1
6
95310
72044
1
6
95310
72044
1
7
99525
82049
1
7
99525
82049
1
8
101600
84306
1
8
102744
85032
1
9
101600
84306
1
9
102744
85032
1
10
104155
86535
1
10
104575
86430
1
11
How would you handle in a pandas dataframe for instance?
You can use a boolean mask. First sort your values by ID_02 then check duplicated values. The position with row set to True has the End position, the other the Start position:
m = df.sort_values('ID_02').duplicated(['X-Coordinate', 'Y-Coordinate', 'ID_01'])
df['Position'] = np.where(m, 'End', 'Start')
print(df)
# Output
X-Coordinate Y-Coordinate ID_01 ID_02 Position
0 45000 554000 15 35 Start
1 45000 554000 15 36 End
Related
list index out of range in calculation of nodal distance
I am working on a small task in which I have to find the distance between two nodes. Each node has X and Y coordinates which can be seen below. node_number X_coordinate Y_coordinate 0 0 1 0 1 1 1 1 2 2 1 2 3 3 1 3 4 4 0 3 5 5 0 4 6 6 1 4 7 7 2 4 8 8 3 4 9 9 4 4 10 10 4 3 11 11 3 3 12 12 2 3 13 13 2 2 14 14 2 1 15 15 2 0 For the purpose I mentioned above, I wrote below code, X1_coordinate = df['X_coordinate'].tolist() Y1_coordinate = df['Y_coordinate'].tolist() node_number1 = df['node_number'].tolist() nodal_dist = [] i = 0 for i in range(len(node_number1)): dist = math.sqrt((X1_coordinate[i+1] - X1_coordinate[i])**2 + (Y1_coordinate[i+1] - Y1_coordinate[i])**2) nodal_dist.append(dist) I got the error list index out of range Kindly let me know what I am doing wrong and what should I change to get the answer.
Indexing starts at zero, so the last element in the list has an index that is one less than the number of elements in that list. But the len() function gives you the number of elements in the list (in other words, it starts counting at 1), so you want the range of your loop to be len(node_number1) - 1 to avoid an -off-by-one error.
The problems should been in this line dist = math.sqrt((X1_coordinate[i+1] - X1_coordinate[i])**2 + (Y1_coordinate[i+1] - Y1_coordinate[i])**2) the X1_coordinate[i+1] and the ] Y1_coordinate[i+1]] go out of range on the last number call.
Checking for subset in a column?
I'm trying to flag some price data as "stale" if the quoted price of the security hasn't changed over lets say 3 trading days. I'm currently trying it with: firm["dev"] = np.std(firm["Price"],firm["Price"].shift(1),firm["Price"].shift(2)) firm["flag"] == np.where(firm["dev"] = 0, 1, 0) But I'm getting nowhere with it. This is what my dataframe would look like. Index Price Flag 1 10 0 2 11 0 3 12 0 4 12 0 5 12 1 6 11 0 7 13 0 Any help is appreciated!
If you are okay with other conditions, you can first check if series.diff equals 0 and take cumsum to check if you have a cumsum of 2 (n-1). Also check if the next row is equal to current, when both these conditions suffice, assign a flag of 1 else 0. n=3 firm['Flag'] = (firm['Price'].diff().eq(0).cumsum().eq(n-1) & firm['Price'].eq(firm['Price'].shift())).astype(int) EDIT, to make it a generalized function with consecutive n, use this: def fun(df,col,n): c = df[col].diff().eq(0) return (c|c.shift(-1)).cumsum().ge(n) & df[col].eq(df[col].shift()) firm['flag_2'] = fun(firm,'Price',2).astype(int) firm['flag_3'] = fun(firm,'Price',3).astype(int) print(firm) Price Flag flag_2 flag_3 Index 1 10 0 0 0 2 11 0 0 0 3 12 0 0 0 4 12 0 1 0 5 12 1 1 1 6 11 0 0 0 7 13 0 0 0
Compare preceding two rows with subsequent two rows of each group till last record
I had a question earlier which is deleted and now modified to a less verbose form for you to read easily. I have a dataframe as given below df = pd.DataFrame({'subject_id' :[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],'day':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] , 'PEEP' :[7,5,10,10,11,11,14,14,17,17,21,21,23,23,25,25,22,20,26,26,5,7,8,8,9,9,13,13,15,15,12,12,15,15,19,19,19,22,22,15]}) df['fake_flag'] = '' I would like to fill values in column fake_flag based on the below rules 1) if preceding two rows are constant (ex:5,5) or decreasing (7,5), then pick the highest of the two rows. In this case, it is 7 from (7,5) and 5 from (5,5) 2) Check whether the current row is greater than the output from rule 1 by 3 or more points (>=3) and it repeats in another (next) row (2 occurrences of same value). It can be 8/gt 8(if rule 1 output is 5). ex: (8 in row n,8 in row n+1 or 10 in row n,10 in row n+1) If yes, then key in fake VAC in the fake_flag column This is what I tried for i in t1.index: if i >=2: print("current value is ", t1[i]) print("preceding 1st (n-1) ", t1[i-1]) print("preceding 2nd (n-2) ", t1[i-2]) if (t1[i-1] == t1[i-2] or t1[i-2] >= t1[i-1]): # rule 1 check r1_output = t1[i-2] # we get the max of these two values (t1[i-2]), it doesn't matter when it's constant(t1[i-2] or t1[i-1]) will have the same value anyway print("rule 1 output is ", r1_output) if t1[i] >= r1_output + 3: print("found a value for rule 2", t1[i]) print("check for next value is same as current value", t1[i+1]) if (t1[i]==t1[i+1]): # rule 2 check print("fake flag is being set") df['fake_flag'][i] = 'fake_vac' This check should happen for all records (one by one) for each subject_id. I have a dataset which has million records. Any efficient and elegant solution is helpful. I can't run a loop over million records. I expect my output to be like as shown below subject_id = 1 subject_id = 2
import pandas as pd df = pd.DataFrame({'subject_id' :[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],'day':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] , 'PEEP' :[7,5,10,10,11,11,14,14,17,17,21,21,23,23,25,25,22,20,26,26,5,7,8,8,9,9,13,13,15,15,12,12,15,15,19,19,19,22,22,15]}) df['shift1']=df['PEEP'].shift(1) df['shift2']=df['PEEP'].shift(2) df['fake_flag'] = np.where((df['shift1'] ==df['shift2']) | (df['shift1'] < df['shift2']), 'fake VAC', '') df.drop(['shift1','shift2'],axis=1) Output 0 1 1 7 1 1 2 5 2 1 3 10 fake VAC 3 1 4 10 4 1 5 11 fake VAC 5 1 6 11 6 1 7 14 fake VAC 7 1 8 14 8 1 9 17 fake VAC 9 1 10 17 10 1 11 21 fake VAC 11 1 12 21 12 1 13 23 fake VAC 13 1 14 23 14 1 15 25 fake VAC 15 1 16 25 16 1 17 22 fake VAC 17 1 18 20 fake VAC 18 1 19 26 fake VAC 19 1 20 26 20 2 1 5 fake VAC 21 2 2 7 fake VAC 22 2 3 8 23 2 4 8 24 2 5 9 fake VAC 25 2 6 9 26 2 7 13 fake VAC
How to calculate amounts that row values greater than a specific value in pandas?
How to calculate amounts that row values greater than a specific value in pandas? For example, I have a Pandas DataFrame dff. I want to count row values greater than 0. dff = pd.DataFrame(np.random.randn(9,3),columns=['a','b','c']) dff a b c 0 -0.047753 -1.172751 0.428752 1 -0.763297 -0.539290 1.004502 2 -0.845018 1.780180 1.354705 3 -0.044451 0.271344 0.166762 4 -0.230092 -0.684156 -0.448916 5 -0.137938 1.403581 0.570804 6 -0.259851 0.589898 0.099670 7 0.642413 -0.762344 -0.167562 8 1.940560 -1.276856 0.361775 I am using an inefficient way. How to be more efficient? dff['count'] = 0 for m in range(len(dff)): og = 0 for i in dff.columns: if dff[i][m] > 0: og += 1 dff['count'][m] = og dff a b c count 0 -0.047753 -1.172751 0.428752 1 1 -0.763297 -0.539290 1.004502 1 2 -0.845018 1.780180 1.354705 2 3 -0.044451 0.271344 0.166762 2 4 -0.230092 -0.684156 -0.448916 0 5 -0.137938 1.403581 0.570804 2 6 -0.259851 0.589898 0.099670 2 7 0.642413 -0.762344 -0.167562 1 8 1.940560 -1.276856 0.361775 2
You can create a boolean mask of your DataFrame, that is True wherever a value is greater than your threshold (in this case 0), and then use sum along the first axis. dff.gt(0).sum(1) 0 1 1 1 2 2 3 2 4 0 5 2 6 2 7 1 8 2 dtype: int64
Divide part of a dataframe by another while keeping columns that are not being divided
I have two data frames as below: Sample_name C14-Cer C16-Cer C18-Cer C18:1-Cer C20-Cer 0 1 1 0.161456 0.033139 0.991840 2.111023 0.846197 1 1 10 0.636140 1.024235 36.333741 16.074662 3.142135 2 1 13 0.605840 0.034337 2.085061 2.125908 0.069698 3 1 14 0.038481 0.152382 4.608259 4.960007 0.162162 4 1 5 0.035628 0.087637 1.397457 0.768467 0.052605 5 1 6 0.114375 0.020196 0.220193 7.662065 0.077727 Sample_name C14-Cer C16-Cer C18-Cer C18:1-Cer C20-Cer 0 1 1 0.305224 0.542488 66.428382 73.615079 10.342252 1 1 10 0.814696 1.246165 73.802644 58.064363 11.179206 2 1 13 0.556437 0.517383 50.555948 51.913547 9.412299 3 1 14 0.314058 1.148754 56.165767 61.261950 9.142128 4 1 5 0.499129 0.460813 40.182454 41.770906 8.263437 5 1 6 0.300203 0.784065 47.359506 52.841821 9.833513 I want to divide the numerical values in the selected cells of the first by the second and I am using the following code: df1_int.loc[:,'C14-Cer':].div(df2.loc[:,'C14-Cer':]) However, this way I lose the information from the column "Sample_name". C14-Cer C16-Cer C18-Cer C18:1-Cer C20-Cer 0 0.528977 0.061088 0.014931 0.028677 0.081819 1 0.780831 0.821909 0.492309 0.276842 0.281070 2 1.088785 0.066367 0.041243 0.040951 0.007405 3 0.122529 0.132650 0.082047 0.080964 0.017738 4 0.071381 0.190178 0.034778 0.018397 0.006366 5 0.380993 0.025759 0.004649 0.145000 0.007904 How can I perform the division while keeping the column "Sample_name" in the resulting dataframe?
You can selectively overwrite using loc, the same way that you're already performing the division: df1_int.loc[:,'C14-Cer':] = df1_int.loc[:,'C14-Cer':].div(df2.loc[:,'C14-Cer':]) This preserves the sample_name col: In [12]: df.loc[:,'C14-Cer':] = df.loc[:,'C14-Cer':].div(df1.loc[:,'C14-Cer':]) df Out[12]: Sample_name C14-Cer C16-Cer C18-Cer C18:1-Cer C20-Cer index 0 1 1 0.528975 0.061087 0.014931 0.028677 0.081819 1 1 10 0.780831 0.821910 0.492309 0.276842 0.281070 2 1 13 1.088785 0.066367 0.041243 0.040951 0.007405 3 1 14 0.122528 0.132650 0.082047 0.080964 0.017738 4 1 5 0.071380 0.190179 0.034778 0.018397 0.006366 5 1 6 0.380992 0.025758 0.004649 0.145000 0.007904