简体   繁体   中英

How to extract entire rows from pandas data frame, if a column's string value contains a specific pattern

I have the following data frame with column 'Name' having a pattern '///' in its values

data = [['a1','yahoo', 'apple'], ['a2','gma///il', 'mango'], ['a3','amazon', 'papaya'], 
['a4','bi///ng', 'guava']] 
df = pd.DataFrame(data, columns = ['ID', 'Name', 'Info']) 

I need to extract the entire row from this data frame if the column 'Name' has Value having a pattern '///' in it. I have tried the following code but getting a empty dataframe.

new_df = df.loc[df['Name'] == '///']

My expected output should give me a data frame like this:

data_new = [['a2','gma///il', 'mango'],['a4','bi///ng', 'guava']] 
new_df = pd.DataFrame(data, columns = ['ID', 'Name', 'Info'])  
print(new_df)

Use Series.str.contains :

import pandas as pd

data = [['a1','yahoo', 'apple'], ['a2','gma///il', 'mango'],
        ['a3','amazon', 'papaya'],['a4','bi///ng', 'guava']]

df = pd.DataFrame(data, columns = ['ID', 'Name', 'Info'])

print (df[df["Name"].str.contains("///")])

#
   ID      Name   Info
1  a2  gma///il  mango
3  a4   bi///ng  guava

If you want to filter on perticular one column then use this solution

import numpy as np
immport pandas as pd

data = [['a1','yahoo', 'apple'], ['a2','gma///il', 'mango'], ['a3','amazon', 'papaya'], 
['a4','bi///ng', 'guava']] 
df = pd.DataFrame(data, columns = ['ID', 'Name', 'Info']) 

mask = np.column_stack([df['Name'].str.contains(r"\///", na=False)]) 
df.loc[mask.any(axis=1)]

Output:

   ID      Name   Info
1  a2  gma///il  mango
3  a4   bi///ng  guava

If you need filtering on all columns for some pattern then see the below solution

import numpy as np
mask = np.column_stack([df[col].str.contains(r"\///", na=False) for col in df]) 
df.loc[mask.any(axis=1)]

Output:

   ID      Name   Info
1  a2  gma///il  mango
3  a4   bi///ng  guava

DataFrame has string function contains() for this

 new_df = df[ df['Name'].str.contains('///') ]

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