I have a dataframe which looks like the following:
I need to select those row in which any of the three angles are not close to 90 or 180 by a value of 35. For example, I need to select rows which have Angle values like: 50, 50, 140.
Please help. Thanks in advance.
def check_angle(df_list):
ANGLE_THRESHOLD = 35
ANGLE_NINETY = 90
ANGLE_ONE_EIGHTY = 180
for df in df_list:
print df.loc[df['Angle'].str.split(',')[0] >= (ANGLE_NINETY + ANGLE_THRESHOLD)]
Till now, I have tried this, but its throwing KeyError.
If df
is your data frame
A B
0 1 92.36,89.1,177.47
1 2 90.49,90.9,178.97
2 3 91.66,92.4,177.08
3 4 94,86.87,174.92
4 5 97.41,85.65,173.8
5 5 20.41,20.65,20.8
you can split it with split(',') and iterate through the rows.
df_tmp = pd.DataFrame([[val.split(",")[0],val.split(",")[1],val.split(",")[2] ] for val in df.B])
The returned boolean can be used to create a new dataframe
df_tmp2 = pd.DataFrame({
"val1in":(df_tmp[0].astype(float)>(90.-35.)) & (df_tmp[0].astype(float)<(90.+35.)) | (df_tmp[0].astype(float)>(180.-35.)) & (df_tmp[0].astype(float)<(180.+35.)),
"val2in":(df_tmp[1].astype(float)>(90.-35.)) & (df_tmp[1].astype(float)<(90.+35.)) | (df_tmp[1].astype(float)>(180.-35.)) & (df_tmp[1].astype(float)<(180.+35.)),
"val3in":(df_tmp[2].astype(float)>(90.-35.)) & (df_tmp[2].astype(float)<(90.+35.)) | (df_tmp[2].astype(float)>(180.-35.)) & (df_tmp[2].astype(float)<(180.+35.))
})
based on which you can select the rows
print (df[np.all(df_tmp2,axis =1)==False])
You can do this:
df=pd.DataFrame({"A":[1,2,3,4],"val":[[140,89.1,177.47],[90.49,90.9,178.90],[91.66,92.4,177.08],[135,85.65,173.8]]})
B=[]
C=[]
D=[]
for i in range(len(df)):
B.append(df["val"][i][0])
C.append(df["val"][i][1])
D.append(df["val"][i][2])
df["B"]=B
df["C"]=C
df["D"]=D
print(df.query("(B < 180-35 & B > 90+35) | (C < 180-35 & C > 90+35) | (D < 180-35 & D > 90+35)"))
A val B C D
0 1 [140, 89.1, 177.47] 140.0 89.10 177.47
3 4 [135, 85.65, 173.8] 135.0 85.65 173.80
For convenience you can also drop the colmns B,C,D after selection
You can use str.split
with expand=True
for DataFrame and cast to float
s, then compare with conditions and last use boolean indexing
with inverted mask by ~
and all
for check all False
s rows:
df=pd.DataFrame({"S/N":[1,2,3,4],
"Angle":['50,50,140','90.49,90.9,178.90',
'91.66,92.4,177.08','135,85.65,173.8']})
print (df)
Angle S/N
0 50,50,140 1
1 90.49,90.9,178.90 2
2 91.66,92.4,177.08 3
3 135,85.65,173.8 4
AT = 35
AN = 90
AO = 180
a = df['Angle'].str.split(',', expand=True).astype(float)
m = ((a > AN-AT) & (a < AN+AT)) | ((a > AO-AT) & (a < AO+AT))
print (m)
0 1 2
0 False False False
1 True True True
2 True True True
3 False True True
df = df[(~m).all(axis=1)]
print (df)
Angle S/N
0 50,50,140 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.