How To Loop The Data With Condition?
I have a set of data such that the 1st column is age (numerical), 2nd column is gender (categorical) and 3rd column is saving (numerical).
What I want to do is find the mean and standard deviation if the column is a numerical data, and find the mode if the column is categorical data.
I tried to find the index if the
type = num and put the index into the
for loop to calculate the mean and standard deviation and the rest of the index is used to calculate the mode of the categorical data (in this case is 2nd column), however, I had stuck in the loop.
import numpy as np data = np.array([[11, "male",1222],[23,"female",333],[15,"male",542]]) # type of the data above types = ["num","cat","num"] idx =  for i in range(2): if (types[i] == "num"): idx.append(types[i].index) for i in idx: np.mean(data[:,i].astype("float64"))
I hope the code is able to obtain the mean and standard deviation for numerical data and mode for categorical data. If it is possible, try not to build in any other package (I'm not sure `index' have it own package or not).
Simply remove the parenthesis in the if statement.
... idx =  for i in range(2): if types[i] == "num": idx.append(types[i].index) ...
Edit: Instead of looping a range I would suggest iterate your types array with enumerate, in that way you have the index of your desired item.
for index, _type in enumerate(types): if _type == 'num': idx.append(index)
- → 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