Generating A Dataframe From A Groupby Transformation
I have this code:
df has 15
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?
IIUC, you could simply use
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
out = df.filter(like='feature').groupby(df['type']).mean().reset_index()
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
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module