Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using List Comprehension with Pandas Series and Dataframes

I have written the below code that accepts a pandas series (dataframe column) of strings and a dictionary of terms to replace in the strings.

def phrase_replace(repl_dict, str_series):
    for k,v in repl_dict.items():
         str_series = str_series.str.replace(k,v)
    return str_series

It works correctly, but it seems like I should be able to use some kind of list comprehension instead of the for loop. I don't want to use str_series = [] or {} because I don't want a list or a dictionary returned, but a pandas.core.series.Series

Likewise, if I want to use the function on every column in a dataframe:

for column in df.columns:
    df[column] = phrase_replace(repl_dict, df[column])

There must be a list comprehension method to do this?

like image 757
AJ AJ Avatar asked Sep 12 '25 10:09

AJ AJ


1 Answers

It is possible, but then need concat for DataFrame because get list of Series:

df = pd.concat([phrase_replace(repl_dict, df[column]) for column in df.columns], axis=1)

But maybe need replace by dictionary:

df = df.replace(repl_dict)
like image 142
jezrael Avatar answered Sep 14 '25 02:09

jezrael