As shown below is a data frame contain Car Price data. The issue is due to there's many Car Model (8 types), the data was not display in a user friendly way. Is there a way to restructure it to following desired output so that the output is more user-friendly.
[Content updated]
PS: Regarding the Car Model , not all car Model present in every year. For instance, for 2000, Car Model Car H was not presented. Using pivot function would result in NaN value. Aside from pivot, is there another way to do it?
Dataframe
import pandas as pd
j = [
{
"Year":"2000",
"Car Model":"Car A",
"Car price":"1000"
},
{
"Year":"2000",
"Car Model":"Car B",
"Car price":"1200"
},
{
"Year":"2000",
"Car Model":"Car C",
"Car price":"1300"
},
{
"Year":"2000",
"Car Model":"Car D",
"Car price":"1100"
},
{
"Year":"2000",
"Car Model":"Car E",
"Car price":"1400"
},
{
"Year":"2000",
"Car Model":"Car F",
"Car price":"1000"
},
{
"Year":"2000",
"Car Model":"Car G",
"Car price":"1200"
},
{
"Year":"2001",
"Car Model":"Car A",
"Car price":"1400"
},
{
"Year":"2001",
"Car Model":"Car C",
"Car price":"1200"
},
{
"Year":"2001",
"Car Model":"Car D",
"Car price":"1300"
},
{
"Year":"2001",
"Car Model":"Car E",
"Car price":"1000"
},
{
"Year":"2001",
"Car Model":"Car F",
"Car price":"1200"
},
{
"Year":"2001",
"Car Model":"Car G",
"Car price":"1200"
},
{
"Year":"2001",
"Car Model":"Car H",
"Car price":"1000"
}
]
Get the mean
out = pd.DataFrame(j).groupby(['Year','Car Model'],as_index=False)[['Car price']].agg(lambda x : x.astype(int).mean())
out
output
Reshape data
res = out.pivot(index='Year', columns='Car Model', values='Car price')
res
output with NaN value
Targeted output