Home > Enterprise >  Filter column data with some keywords in each cell using paython pandas
Filter column data with some keywords in each cell using paython pandas

Time:01-09

I have a Excel sheet so I am creating an Automation script which takes all the data containing some specific key words. Lets say in column C I have many names then I need to filter only those rows in which the data starts with term "John" with different surnames. If in Column C 1000 names are there and 100 names start with first name John with different surnames. So I want all the 100 name along with corresponding Column A, B and D. I am not able to filter out on column.

CodePudding user response:

You can use startswith - try out this example:

df = pd.DataFrame(np.array([[1, 2, 'John A', 3], [1, 2, 'John B', 3], [1, 2, 'Michael A', 3]]), columns=['A', 'B', 'C', 'D'])

df_new = df[df.C.str.startswith('John ')]

CodePudding user response:

df.loc[df['C'].str.contains('John')]
  •  Tags:  
  • Related