admin管理员组文章数量:1245084
import pandas as pd import numpy as np df= pd.read_csv ("file.csv") n = 0 a = 7 # 1st value to find in row1 b = 39 # 2nd value to find in row2 c = 10 # 3rd value to find in row3 d = 38 # 4th value to find in row4 value_1 = 5 value_2 = 21 while n < len(df)-1 : if df.loc[n].isin([a]).any # 7 is in any in n row if df.loc[n+1].isin([b]).any(): #39 is in any in n+1 row if df.loc[n+2].isin([c]).any(): #10 is in any in n+2 row if df.loc[n+3].isin([d]).any(): #44 is in any in n+3 row if df.loc[n].isin([value_1]).any(): if (df.loc[n].isin([value_2 ]).any() or df.loc[n-1].isin([value_2 ]).any() or df.loc[n+1].isin([value_2 ]).any()): display (df.loc[n:n]) elif df.loc[n+1].isin([value_1]).any() : if (df.loc[n+1].isin([value_2 ]).any() or df.loc[n].isin([value_2 ]).any() or df.loc[n+1].isin([value_2 ]).any()): display (df.loc[n:n]) elif df.loc[n-1].isin([value_1]).any() : if (df.loc[n-1].isin([value_2 ]).any() or df.loc[n-2].isin([value_2 ]).any() or df.loc[n].isin([value_2 ]).any()): display (df.loc[n:n])
I am very beginner in python. Sorry for the mess. I am trying to find a,b,c,d to find in row 1,2,3,4 from data and once it matches from the data , then if value_1 be in row n and value_2 is in row n or n-1 or n+1 if (df.loc[n].isin([value_2 ]).any() or df.loc[n-1].isin([value_2 ]).any() or df.loc[n+1].isin([value_2 ]).any()):
then show the data. and then same again one below and one above. The code is running but not finding all the matched datas but few. Have i done any mistake using or operator?
For example, first it will find the value a(7) from any row, (here in found in row 4) once it found then more to next row to find b (39 found in row 5), and then c(10 found in row 6) and d(38 found in row 7). Once it found, then it will search for value_1 (5) in the row of a(7 which was found in row 4) or one above(row 3) or below (row 5)
A B C D E F 1 | 4 5 8 16 5 44 #VALUE_1 (5) 2 | 1 4 12 15 22 21 #VALUE_2 (21) 3 | 2 11 13 15 28 33 4 | 5 15 29 35 7 33 5 | 6 7 39 12 14 16 6 | 1 9 10 12 20 26 7 | 6 38 20 36 33 34 8 | 12 21 7 28 35 39 9 | 5 18 39 33 35 36 10| 4 7 10 18 19 28 11| 3 4 15 19 28 38
and later when value_1 (5) is found, then it search for value_2 (21) on the row of value_1 (which was found in row 4) or one above (row 3) or below (row 5). if found then, it will print the row 1, and continue to find more match
import pandas as pd import numpy as np df= pd.read_csv ("file.csv") n = 0 a = 7 # 1st value to find in row1 b = 39 # 2nd value to find in row2 c = 10 # 3rd value to find in row3 d = 38 # 4th value to find in row4 value_1 = 5 value_2 = 21 while n < len(df)-1 : if df.loc[n].isin([a]).any # 7 is in any in n row if df.loc[n+1].isin([b]).any(): #39 is in any in n+1 row if df.loc[n+2].isin([c]).any(): #10 is in any in n+2 row if df.loc[n+3].isin([d]).any(): #44 is in any in n+3 row if df.loc[n].isin([value_1]).any(): if (df.loc[n].isin([value_2 ]).any() or df.loc[n-1].isin([value_2 ]).any() or df.loc[n+1].isin([value_2 ]).any()): display (df.loc[n:n]) elif df.loc[n+1].isin([value_1]).any() : if (df.loc[n+1].isin([value_2 ]).any() or df.loc[n].isin([value_2 ]).any() or df.loc[n+1].isin([value_2 ]).any()): display (df.loc[n:n]) elif df.loc[n-1].isin([value_1]).any() : if (df.loc[n-1].isin([value_2 ]).any() or df.loc[n-2].isin([value_2 ]).any() or df.loc[n].isin([value_2 ]).any()): display (df.loc[n:n])
I am very beginner in python. Sorry for the mess. I am trying to find a,b,c,d to find in row 1,2,3,4 from data and once it matches from the data , then if value_1 be in row n and value_2 is in row n or n-1 or n+1 if (df.loc[n].isin([value_2 ]).any() or df.loc[n-1].isin([value_2 ]).any() or df.loc[n+1].isin([value_2 ]).any()):
then show the data. and then same again one below and one above. The code is running but not finding all the matched datas but few. Have i done any mistake using or operator?
For example, first it will find the value a(7) from any row, (here in found in row 4) once it found then more to next row to find b (39 found in row 5), and then c(10 found in row 6) and d(38 found in row 7). Once it found, then it will search for value_1 (5) in the row of a(7 which was found in row 4) or one above(row 3) or below (row 5)
A B C D E F 1 | 4 5 8 16 5 44 #VALUE_1 (5) 2 | 1 4 12 15 22 21 #VALUE_2 (21) 3 | 2 11 13 15 28 33 4 | 5 15 29 35 7 33 5 | 6 7 39 12 14 16 6 | 1 9 10 12 20 26 7 | 6 38 20 36 33 34 8 | 12 21 7 28 35 39 9 | 5 18 39 33 35 36 10| 4 7 10 18 19 28 11| 3 4 15 19 28 38
and later when value_1 (5) is found, then it search for value_2 (21) on the row of value_1 (which was found in row 4) or one above (row 3) or below (row 5). if found then, it will print the row 1, and continue to find more match
Share Improve this question edited Feb 17 at 10:46 Kan asked Feb 16 at 16:06 KanKan 154 bronze badges 2- Please provide a minimum example which shows the output required from defined input data (see minimal reproducible example). The code you provide will not run and the logic is not clear. – user19077881 Commented Feb 16 at 18:29
- suggest this question is closed and the OP encouraged write down WHAT they are attempting (perhaps in pseudocode), removing the code they've written. – ticktalk Commented Feb 17 at 12:53
1 Answer
Reset to default 1I'm not sure I understood your question completely correctly, but here is one way to accomplish the task:
import pandas as pd
df = pd.read_csv("file.csv", delimiter=',')
a = 7 # 1st value
b = 39 # 2nd value
c = 10 # 3rd value
d = 38 # 4th value
value_1 = 5
value_2 = 21
for i, row in df.iterrows():
if (i < len(df) - 3 and # checks if there are 3 more lines after
a in row.unique() and # checks a in current line
b in df.loc[i + 1].unique() and # checks b in line with index [i + 1]
c in df.loc[i + 2].unique() and # checks c in line with index [i + 2]
d in df.loc[i + 3].unique()): # checks d in line with index [i + 3]
for j in range(i - 1, i + 2): # check is value_1 in rows [i - 1], [i], [i + 1]
if value_1 in df.loc[j].unique():
for k in range(j - 1, j + 2): # check is value_2 in rows [j - 1], [j], [j + 1]
if value_2 in df.loc[k].unique():
print(row)
I left explanations of the code in the comments.
本文标签: Find value from rowabove and below using If statements in pythonStack Overflow
版权声明:本文标题:Find value from row , above and below using If statements in python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740200869a2240189.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论