kieranm
kieranm

Reputation: 31

Take the mean of a series of lists and get the mean

I am working with a series of strings. I pulled out the numbers from those strings and want to get the average. The problem seems to be that they are still strings and I am not sure how to turn each item in the list in the series into an integer and then take the mean.

list_series = added_df["Salary Estimate"].str.findall(r'\d+')
for index, value in list_series.items():
    for i in value:
        i = int(i)       
from statistics import mean
added_df['Average Salary'] = added_df['Salary Estimate'].map(mean)

I thought that I had converted them into integers, but I still get this error message:

TypeError: can't convert type 'str' to numerator/denominator

Upvotes: 3

Views: 64

Answers (1)

BENY
BENY

Reputation: 323276

You can do explode then groupby

added_df["Salary Estimate mean"] = added_df["Salary Estimate"].str.findall(r'\d+').explode().astype(int).groupby(level=0).mean()

Or

added_df["Salary Estimate"].str.findall(r'\d+').apply(pd.Series).astype(int).mean(1)

Upvotes: 2

Related Questions