2

i'm trying to convert a column of prices in a dataframe into float and then calculate the mean of the first 5 rows. first i did it succussefully in this way :

import pandas as pd
import numpy as np
paris_listing = pd.read_csv("C:../.../.../paris_airbnb.csv")
stripped_commas = paris_listing["price"].str.replace(",", "")
stripped_dollars = stripped_commas.str.replace("$", "")
paris_listing["price"] = stripped_dollars.astype("float")
mean_price = paris_listing.iloc[0:5]["price"].mean()
print (mean_price)

but i tried to make a function and apply it on the dataframe and it didn't work

def conversion_price(price_conv):
    price_conv = price_conv.str.replace(",", "")
    price_conv = price_conv.str.replace("$", "")
    price_conv = price_conv.astype("float")
    price_mean = price_conv.iloc[0:5].mean()
paris_listing["converted_price"] = paris_listing["price"].apply(conversion_price)
3
  • 1
    What is your specific error? Please provide all relevant details, even the output of paris_listing.head() if feasible. Commented Aug 25, 2020 at 0:37
  • This is a pandas question, and has nothing to do with machine-learning (tags edited). Commented Aug 25, 2020 at 8:22
  • if you want to assign to other column then in function you fogot return price_conv Commented Aug 25, 2020 at 10:47

3 Answers 3

0

Could you please try below instead of second and third line of function

price_conv = float(price_conv.replace("$", ""))

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

1 Comment

Thank you for the anwer, well, my aim is to create a function to convert all the rows and then calculate the mean of the 5 first rows
0

Thank you for your help :) i tried this function and it works well

def convert_price (df):
    df = df.replace("$", "")
    df = df.replace(",", "")
    df = float(df)
    
    return df
converted_price = paris_listing["price"].apply(convert_price)
paris_listing["price"].head()
converted_price.head()

and i got this result :

1956     80.0
3735     67.0
6944     36.0
2094    120.0
2968     60.0
Name: price, dtype: float64
1956     80.0
3735     67.0
6944     36.0
2094    120.0
2968     60.0
Name: price, dtype: float64

otherwise i'd like to calculate the mean of the series(the result) but when i use

mean_price = df.mean()

i get this error : AttributeError: 'float' object has no attribute 'mean'

1 Comment

All you have to do is convert the price column to a float in the following manner: df['price'] = df['price'].str.replace(r'[$\,]','').astype('float') and then you can calculate mean of first 5 rows in the following manner: mean = df['price'].iloc[0:5].mean()
0

Your question is a bit confusing, do you want all the rows to have mean of first 5 prices or the mean of next five prices? Anyway here's the code to calculate the mean for next 5 prices. The get_mean function will return mean(present_index to present_index+5).

def get_mean(row):
    
    index = df[df == row].dropna().index
    if index+4 in df.index:
        index_list = range(index,index+5)
        price_mean = np.mean([df.loc[index,'price'] for index in index_list])
        return price_mean
    return np.NaN

paris_listing['price'] = paris_listing['price'].str.replace(r'[$\,]','').astype('float')
paris_listing["converted_price"] = paris_listing.apply(get_mean,axis = 1)

Following statement can be used to find the mean of just the first 5 rows

mean = df.price[0:5].mean()

4 Comments

I would suggest attaching desired output and a df.head() to your question.
Thank you for the anwer, well, my aim is to create a function to convert all the rows and then calculate the mean of the 5 first rows. i will try your code and give you back the head()
While you're at it, please attach expected output also.
Checkout the new edit, I think this will solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.