Data Science Syntax from DataCamp

 for key,value in europe.items()

Use .items to iterate in a dictionary using for loop


If you're dealing with a 2D Numpy array, it's more complicated. A 2D array is built up of multiple 1D arrays. To explicitly iterate over all separate elements of a multi-dimensional array, you'll need this syntax:

for x in np.nditer(my_array) :
for a in np.nditer(np_baseball):
    print(a)f
Iterating over a Pandas DataFrame is typically done with the iterrows() method. Used in a for loop, every observation is iterated over and on every iteration the row label and actual row contents are available:
Eg - for lab, row in cars.iterrows():
    print(lab)
    print(row)
 # Code for loop that adds COUNTRY column
for lab, row in cars.iterrows():
    cars.loc[lab, 'COUNTRY'] = (row['country'].upper())
 # Use .apply(str.upper)
for lab, row in cars.iterrows() :
    cars["COUNTRY"]  = cars['country'].apply(str.upper)

Comments

Popular posts from this blog

Binomial Test in Python

Python Syntax and Functions Part2 (Summary Statistics)

Slicing and Indexing in Python Pandas