Drop a row or observation by condition
Question:
Let’s assume we have a dataframe df(1200, 20). Since I am only interested in 2 columns in the I will use only these in the following example
index A B
1 Alex George
2 Paul Patrick
3 A.S. Nick
3 Alice Dave
I am interested in dropping the rows that have only A.S. in my column A. Basically to obtain the following result
index A B
1 Alex George
2 Paul A.S. Patrick
3 Alice Dave
I tried the following
df2 = df1[df1['A'] != 'A.S.']
but it does not seem to do the trick. Any suggestions?
Answers:
Have you tried?
df2 = df1[~df1['A'].str.contains('A.S. ')]
You may refer to this answer for the details.
Let’s assume we have a dataframe df(1200, 20). Since I am only interested in 2 columns in the I will use only these in the following example
index A B
1 Alex George
2 Paul Patrick
3 A.S. Nick
3 Alice Dave
I am interested in dropping the rows that have only A.S. in my column A. Basically to obtain the following result
index A B
1 Alex George
2 Paul A.S. Patrick
3 Alice Dave
I tried the following
df2 = df1[df1['A'] != 'A.S.']
but it does not seem to do the trick. Any suggestions?
Have you tried?
df2 = df1[~df1['A'].str.contains('A.S. ')]
You may refer to this answer for the details.