I have this code:
df.droupby('type)['feature1].mean()
df
has 15 features
from 1 to 15
I want to iterate over a list of the features names and append these 15 results in a dataframe:
Desired Output: type(dataframe)
type feature1 features2 ..... feature15
type_A mean(float) mean(float) mean(float)
type_B mean(float) mean(float) mean(float)
type_c mean(float) mean(float) mean(float)
What I did:
I have the list of features:
list = df.iloc[:, 10:24].columns.to_list()
and tried something like this:
for i in len(list):
df.groupby('type')[list[i]].mean()
to see if I get something and this line returns an error:
'int' object is not iterable
Can anyone help me with this?
CodePudding user response:
IIUC, you could simply use groupby
mean
:
out = df.groupby('type', as_index=False).mean()
But if you have a bunch of other columns that you don't want to include in the calculation and only want the mean of "feature..." columns, you could filter
, then groupby
mean
:
out = df.filter(like='feature').groupby(df['type']).mean().reset_index()
Output:
type feature1 feature2
0 A 10.0 11.0
1 B 12.0 14.0
2 C 13.0 13.0
3 D 12.0 19.0
4 E 10.0 10.0