pythonpandasdataframejupyter-notebook

How do I transfer data from pandas Dataframe to a Dataframe variable of a different type


So I have a Dataframe of car data which has a price column. So I have a series of car data which has a price column.

So I want to transfer the data from the price column a variable which is a Dataframe which has the same data as the original column, but I don't want to modify the original Dataframe. So far I have tried this

# Group columns of the car sales DataFrame by the Make column and find the average
price_list = car_sales["Price"].str.replace("$", "").replace(",", "")
price_data = pd.to_numeric(price_list, errors="coerce")
car_sales.groupby("Make")[price_data].mean()

The issue is that while I am getting the cleaned data contained in price_list is not in the correct format to be passed in as an argument to the to_numeric function. The to_numeric function keeps returning NaN where the price data should be.

enter image description here


Solution

  • I think the problem was probably the fact that I was using the replace function wrong, but anyway I have found another way to do this which is to use the copy function.

    car_sales_cp = car_sales.copy()
    car_sales_cp["Price"] = car_sales_cp["Price"].str.replace(r"[$,]", "", regex=True)
    car_sales_cp["Price"] = pd.to_numeric(car_sales_cp["Price"])
    car_sales_cp.groupby("Make")["Price"].mean()