Trying to covert pandas dataframe to usable form

This is one column I have extracted from a large pandas dataframe df:

dist=df['How far is your place of education/work from your residence (in km) ?']
dist

0       6-7
1       15+
2     14-15
3       15+
4       15+
      ...  
71      1-2
72      0-1
73      4-5
74      0-1
75      0-1

I'd like to assign 6-7 as 6.5, 1-2 as 1.5 and so on. (15+ to 15) so that I can proceed to do calculate the mean which I need for my project. So this is how I want it as:

0           6.5
1           15
2           14.5
3           15
4           15
.
.
.
71          1.5
72          0.5

How do I do this?

Upvotes: 0

Views: 32

Answers (1)

BENY
BENY

Reputation: 323326

Assume your columns is x , then we do findall and explode get the mean

df.x.str.findall('\d+').explode().astype(float).mean(level=0)
0      6.5
1     15.0
2     14.5
3     15.0
4     15.0
71     1.5
72     0.5
73     4.5
74     0.5
75     0.5
Name: x, dtype: float64

Upvotes: 2

Related Questions