I am making some script, wherein my requirement is to remove some special characters from column names and to make data frame as multi header. code
import pandas as pd
import numpy as np
cars = {'day':['aug','aug','sep','sep','aug'],
'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4','Hyundai Elite i20'],
'Type':['sedan,','sedan','hatchback','hatchback','hatchback'],
'Down Price': [22000,25000,27000,35000,10000]
}
df = pd.DataFrame(cars, columns = ['day','Brand', 'Type','Down Price'])
dfpivot=pd.pivot_table(df,index=['day'],columns=['Brand','Type'],values=['Down Price'],aggfunc=np.max)
dfpivot=pd.DataFrame(dfpivot.to_records())
Post this my requirement is to replace "(" & ")" from column name & to split column name with "," to make it multi header What i tried :
dfpivot.columns=dfpivot.columns.str.replace("(","").replace(")","")
dfpivot.columns = dfpivot.columns.str.split(',', expand=True)
I am getting the below error, I also tried to check similar questions posted, but it is not helping, will be grateful if someone can help to resolve.
AttributeError Traceback (most recent call last) in 10 dfpivot=pd.pivot_table(df,index=['day'],columns=['Brand','Type'],values=['Down Price'],aggfunc=np.max) 11 dfpivot=pd.DataFrame(dfpivot.to_records()) ---> 12 dfpivot.columns=dfpivot.columns.str.replace("(","").replace(")","") 13 dfpivot
AttributeError: 'Index' object has no attribute 'replace'