Wrong result when comparing two columns of data in python

This is my data.

df contains no values onlly column names,

P1  |P2 |P3



df4,

    Names   Std
0   Kumar   10
1   Ravi    5



mask=df4["Names"].str.contains(('|').join(df["P1"].values.tolist()),na=False)

Out[30]:
 0    True
 1    True
Name: Names, dtype: bool

Why does it give true meaning when "P!" does the column have no meaning in it?

+4
source share
1 answer

EDIT If you want to return Falsefor an empty column, you can add a condition to check if the column is not empty :

df = pd.DataFrame(columns=['P1','P2','P3'])
print (df)
Empty DataFrame
Columns: [P1, P2, P3]
Index: []

df4 = pd.DataFrame({'Names':['Kumar','Ravi']})

mask=df4["Names"].str.contains(('|').join(df["P1"].values.tolist()),na=False)
mask = mask & (not df['P1'].empty)
print (mask)
0    False
1    False
Name: Names, dtype: bool
df = pd.DataFrame({'P1':['Kumar']}, columns=['P1','P2','P3'])
print (df)
      P1   P2   P3
0  Kumar  NaN  NaN

df4 = pd.DataFrame({'Names':['Kumar','Ravi']})

mask=df4["Names"].str.contains(('|').join(df["P1"].values.tolist()),na=False)
mask = mask & (not df['P1'].empty)
print (mask)
0     True
1    False
Name: Names, dtype: bool
+1
source

Source: https://habr.com/ru/post/1682636/


All Articles