1

I want to find a way to recreate the same command code from R in Python using something similar as the dplyr package. In R I would do this:

 library(dplyr) df <- data.frame(Countries=c('Brazil','Venezuela','Brazil, Colombia, Paraguay','Argentina','Peru','Andorra,Argentina,Chile,Uruguay'), Code=c(1,2,3,4,5,6)) df %>% filter(grepl('(Brazil|Argentina)',Countries)) 

Or even:

 a=strsplit(as.character(df$Countries),',') a=lapply(a,FUN=function(t) gsub(" ","",t)) ele=unlist(lapply(a,FUN=function(t) any(t%in%c('Brazil','Argentina')))) (df[ele,]) 

The output that I want:

 Countries Code 1 Brazil 1 2 Brazil, Colombia, Paraguay 3 3 Argentina 4 4 Argentina,Chile,Uruguay 6 

In Python I've tried this:

import pandas as pd df = pd.DataFrame(dict(Countries=['Brazil','Venezuela','Brazil, Colombia, Paraguay','Argentina','Peru','Andorra,Argentina,Chile,Uruguay'], Code=[1,2,3,4,5,6])) list_=['Brazil','Argentina'] print(df.loc[df['Countries'].isin(list_)]) 

But the output looks like:

 Countries Code 0 Brazil 1 3 Argentina 4 
0

1 Answer 1

2

Seems like you're looking for the .str extension of pd.Series with an object dtype (essentially you can call pd.Series.str.... for a subset of pandas functions specifically for dealing with regular expression and other string based operations- however this only works if the array is of dtype "object".

mask = df["Countries"].str.contains("Brazil|Argentina") subset = df.loc[mask] print(subset) Countries Code 0 Brazil 1 2 Brazil, Colombia, Paraguay 3 3 Argentina 4 5 Andorra,Argentina,Chile,Uruguay 6 

A neat way you can use this is by using the .join function on your list_ variable to join it into a single string usable by regular expression matching patterns.

list_=['Brazil','Argentina'] pattern = "|".join(list_) # Now we have "Brazil|Argentina" as a string mask = df["Countries"].str.contains(pattern) subset = df.loc[mask] # Same subset as the previous example 

See the docs and other methods besides .str.contains at the documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.