Reputation: 383
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)
Upvotes: 1
Views: 2833
Reputation: 142651
DataFrame
has string function contains()
for this
new_df = df[ df['Name'].str.contains('///') ]
Upvotes: 0
Reputation: 637
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
Upvotes: 0
Reputation: 22493
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
Upvotes: 3