python : conversion a dataframe column with commas and $ into float

Viewed 128

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 Answers

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()

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

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

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'

Related