I have a df:
df =
c1 c2 c3 c4 c5
0 K 6 nan Y V
1 H nan g 5 nan
2 U B g Y L
And a string
s = 'HKg5'
I want to return rows where s[0]=value of c1, s[1]=value of c2, ..... + in some cases where s[i]=nan.
For example, row 1 in df above matches with the string
row 1=
c1 c2 c3 c4 c5
1 H nan g 5 nan
match=True, regardless of s[1,4]=nan
s = H K g 5
And also the string length is dynamic, so my df cols go above c10
I am using df.apply but I can't figure it out clearly. I want to write a function to pass to df.apply, passing the string at the same time.
Thanks for any help!
Output from Chris's answer
df=
c1 c2 c3 c4 c5
0 K 6 NaN Y V
1 H NaN g 5 NaN
2 U B g Y L
s = 'HKg5'
s1 = pd.Series(list(s), index=[f'c{x+1}' for x in range(len(s))])
df.loc[((df == s1) | (df.isna())).all(1)]
Output
`c1 c2 c3 c4 c5`