简体   繁体   中英

Find rows which have one column's value as substring in another column along with other OR conditions in pandas

df = pd.DataFrame({'A':[1,2,3,4], 'B':['abc', 'def', 'pqr', 'xyz'], 'C':['a', 'h', 'm', 'z'], 'D':['g', 'h', 'i', 'j']})

I want to print rows that have C as a substring in B OR C equal to D Something like:

result_df = mydf[(mydf['C']==mydf['D']) | (mydf.B.str.contains(mydf.C, na = False))]

Any help is appreciated!

You can only do this by going through column B and C:

print (df.loc[[y in x for x, y in zip(df["B"], df["C"])]|df["C"].eq(df["D"])])

   A    B  C  D
0  1  abc  a  g
1  2  def  h  h
3  4  xyz  z  j

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM