I want to find average/mean of specific value in specific column, the value is string
Lets say this is the table
RAM Screen_Size Resolution SSD HDD Weight Price
8 15.6 1920 x 1080 512 512 1.90 807.00
12 17.0 2560 x 1600 1000 512 2.00 1500.00
64 15.0 1920 x 1080 512 256 2.50 2000.00
32 14.0 2560 x 1600 1000 512 2.30 1750.00
I want to know the average of all column for specific value of 1920 x 1080
so the output will be like this (maybe)
RAM Screen_Size Resolution SSD HDD Weight Price
1920 x 1080 xxx xxxx xxxx xx xxx xxxx xxxx
CodePudding user response:
IIUC, use:
df.groupby('Resolution', as_index=False).mean()
Output:
Resolution RAM Screen_Size SSD HDD Weight Price
0 1920 x 1080 36.0 15.3 512.0 384.0 2.20 1403.5
1 2560 x 1600 22.0 15.5 1000.0 512.0 2.15 1625.0
CodePudding user response:
new_data = []
for key, sample in df.groupby('Resolution'):
del sample['Resolution']
sample = sample.apply(lambda x: x.astype(float))
new_data.append([key] sample.mean().values.tolist())
new_df = pd.DataFrame(new_data)
new_df.columns = ['Resolution', 'RAM',
'Screen_Size', 'SSD', 'HDD', 'Weight', 'Price']
new_df
Output
Resolution RAM Screen_Size SSD HDD Weight Price
0 1920x1080 36.0 15.3 512.0 384.0 2.20 1403.5
1 2560x1600 22.0 15.5 1000.0 512.0 2.15 1625.0